2005-12-22 John Luke <john.luke@gmail.com>
[mono-project.git] / mcs / class / System / System.ComponentModel / EnumConverter.cs
blob9989ca037e8dc6bf72e4fd21f88978538abe3b80
1 //
2 // System.ComponentModel.EnumConverter.cs
3 //
4 // Authors:
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) 2002 Ximian, Inc (http://www.ximian.com)
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System;
33 using System.Collections;
34 using System.Globalization;
35 using System.Reflection;
36 using System.ComponentModel.Design.Serialization;
38 namespace System.ComponentModel
40 public class EnumConverter : TypeConverter
42 private Type type;
43 private StandardValuesCollection stdValues;
45 public EnumConverter (Type type)
47 this.type = type;
50 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
52 if (destinationType == typeof (InstanceDescriptor))
53 return true;
55 return base.CanConvertTo (context, destinationType);
58 public override object ConvertTo (ITypeDescriptorContext context,
59 CultureInfo culture,
60 object value,
61 Type destinationType)
63 if (destinationType == typeof (string))
64 if (value != null)
65 return Enum.Format (type, value, "G");
67 if (destinationType == typeof (InstanceDescriptor) && type.IsInstanceOfType (value)) {
68 FieldInfo f = type.GetField (value.ToString ());
69 if (f == null) throw new ArgumentException (string.Format ("The value '{0}' is not a valid value for the enum '{1}'", value, type));
70 return new InstanceDescriptor (f, null);
73 return base.ConvertTo (context, culture, value, destinationType);
76 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
78 if (sourceType == typeof (string))
79 return true;
80 return base.CanConvertFrom (context, sourceType);
83 public override object ConvertFrom (ITypeDescriptorContext context,
84 CultureInfo culture,
85 object value)
87 string val = value as string;
88 if (val == null)
89 return base.ConvertFrom(context, culture, value);
91 return Enum.Parse (type, val, true);
94 public override bool IsValid (ITypeDescriptorContext context, object value)
96 return Enum.IsDefined (type, value);
99 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
101 return true;
104 public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
106 return !(type.IsDefined (typeof (FlagsAttribute), false));
109 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
111 if (stdValues == null) {
112 Array values = Enum.GetValues (type);
113 Array.Sort (values);
114 stdValues = new StandardValuesCollection (values);
116 return stdValues;
119 protected virtual IComparer Comparer {
120 get { return new EnumConverter.EnumComparer (); }
123 protected Type EnumType {
124 get { return type; }
127 protected TypeConverter.StandardValuesCollection Values {
128 get { return stdValues; }
129 set { stdValues = value; }
132 private class EnumComparer : IComparer
134 int IComparer.Compare (object compareObject1, object compareObject2)
136 string CompareString1 = (compareObject1 as string);
137 string CompareString2 = (compareObject2 as string);
138 if ((CompareString1 == null) || (CompareString2 == null))
139 return Collections.Comparer.Default.Compare (compareObject1, compareObject2);
140 return CultureInfo.InvariantCulture.CompareInfo.Compare (CompareString1, CompareString2);