Fixed space indents to tabs to adhere to Mono coding guidelines.
[mono.git] / mcs / class / System / System.ComponentModel / GuidConverter.cs
1 //
2 // System.ComponentModel.GuidConverter
3 //
4 // Author:
5 //  Andreas Nahr (ClassDevelopment@A-SoftTech.com)
6 //
7 // (C) 2003 Andreas Nahr
8 //
9
10 //
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.
29 //
30
31 using System.Globalization;
32 using System.Reflection;
33 using System.ComponentModel.Design.Serialization;
34
35 namespace System.ComponentModel
36 {
37         public class GuidConverter : TypeConverter
38         {
39
40                 public GuidConverter()
41                 {
42                 }
43
44                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
45                 {
46                         if (sourceType == typeof (string)) 
47                                 return true;
48                         return base.CanConvertFrom (context, sourceType);
49                 }
50
51                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
52                 {
53                         if (destinationType == typeof (string))
54                                 return true;
55                         if (destinationType == typeof (InstanceDescriptor))
56                                 return true;
57                         return base.CanConvertTo (context, destinationType);
58                 }
59
60                 public override object ConvertFrom (ITypeDescriptorContext context,
61                                                     CultureInfo culture, object value)
62                 {
63                         if (value.GetType() == typeof (string)) {
64                                 string GuidString = (string) value;
65                                 try {
66                                         return new Guid (GuidString);
67                                 } catch {
68                                         throw new FormatException (GuidString + "is not a valid GUID.");
69                                 }
70                         }
71                         return base.ConvertFrom (context, culture, value);
72                 }
73
74                 public override object ConvertTo (ITypeDescriptorContext context,
75                                                   CultureInfo culture, object value, Type destinationType)
76                 {
77                         if (value is Guid) {
78                                 Guid guid = (Guid) value;
79
80                                 if (destinationType == typeof (string) && value != null)
81                                         // LAMESPEC MS seems to always parse "D" type
82                                         return guid.ToString("D");
83
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") });
87                                 }
88                         }
89                         return base.ConvertTo (context, culture, value, destinationType);
90                 }
91         }
92 }