2 // System.ComponentModel.EnumConverter.cs
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
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
43 private StandardValuesCollection stdValues
;
45 public EnumConverter (Type type
)
50 public override bool CanConvertTo (ITypeDescriptorContext context
, Type destinationType
)
52 if (destinationType
== typeof (InstanceDescriptor
))
55 return base.CanConvertTo (context
, destinationType
);
58 public override object ConvertTo (ITypeDescriptorContext context
,
63 if (destinationType
== typeof (string))
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))
80 return base.CanConvertFrom (context
, sourceType
);
83 public override object ConvertFrom (ITypeDescriptorContext context
,
87 string val
= value as string;
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
)
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
);
114 stdValues
= new StandardValuesCollection (values
);
119 protected virtual IComparer Comparer
{
120 get { return new EnumConverter.EnumComparer (); }
123 protected Type EnumType
{
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
);