2 // System.ComponentModel.GuidConverter
5 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 // (C) 2003 Andreas Nahr
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Globalization;
32 using System.Reflection;
33 using System.ComponentModel.Design.Serialization;
35 namespace System.ComponentModel
37 public class GuidConverter : TypeConverter
40 public GuidConverter()
44 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
46 if (sourceType == typeof (string))
48 return base.CanConvertFrom (context, sourceType);
51 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
53 if (destinationType == typeof (string))
55 if (destinationType == typeof (InstanceDescriptor))
57 return base.CanConvertTo (context, destinationType);
60 public override object ConvertFrom (ITypeDescriptorContext context,
61 CultureInfo culture, object value)
63 if (value.GetType() == typeof (string)) {
64 string GuidString = (string) value;
66 return new Guid (GuidString);
68 throw new FormatException (GuidString + "is not a valid GUID.");
71 return base.ConvertFrom (context, culture, value);
74 public override object ConvertTo (ITypeDescriptorContext context,
75 CultureInfo culture, object value, Type destinationType)
78 Guid guid = (Guid) value;
80 if (destinationType == typeof (string) && value != null)
81 // LAMESPEC MS seems to always parse "D" type
82 return guid.ToString("D");
84 if (destinationType == typeof (InstanceDescriptor)) {
85 ConstructorInfo ctor = typeof(Guid).GetConstructor (new Type[] {typeof(string)});
86 return new InstanceDescriptor (ctor, new object[] { guid.ToString("D") });
89 return base.ConvertTo (context, culture, value, destinationType);