**** Merged from MCS ****
[mono-project.git] / mcs / class / System / System.ComponentModel / GuidConverter.cs
blobe3f0a89fd7007d56b281d24c45d0f43f1279d031
1 //
2 // System.ComponentModel.GuidConverter
3 //
4 // Author:
5 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
6 //
7 // (C) 2003 Andreas Nahr
8 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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;
33 namespace System.ComponentModel
35 public class GuidConverter : TypeConverter
38 public GuidConverter()
42 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
44 if (sourceType == typeof (string))
45 return true;
46 return base.CanConvertFrom (context, sourceType);
49 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
51 if (destinationType == typeof (string))
52 return true;
53 return base.CanConvertTo (context, destinationType);
56 public override object ConvertFrom (ITypeDescriptorContext context,
57 CultureInfo culture, object value)
59 if (value.GetType() == typeof (string)) {
60 string GuidString = (string) value;
61 try {
62 return new Guid (GuidString);
63 } catch {
64 throw new FormatException (GuidString + "is not a valid GUID.");
67 return base.ConvertFrom (context, culture, value);
70 public override object ConvertTo (ITypeDescriptorContext context,
71 CultureInfo culture, object value, Type destinationType)
73 if (destinationType == typeof (string) && value != null &&value.GetType() == typeof (Guid))
74 // LAMESPEC MS seems to always parse "D" type
75 return ((Guid) value).ToString("D");
76 return base.ConvertTo (context, culture, value, destinationType);