2 // System.ComponentModel.EnumConverter.cs
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 // Ivan N. Zlatev (contact@i-nz.net)
9 // (C) 2002 Ximian, Inc (http://www.ximian.com)
10 // (C) 2003 Andreas Nahr
11 // (C) 2007 Ivan N. Zlatev
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections;
36 using System.Globalization;
37 using System.Reflection;
38 using System.ComponentModel.Design.Serialization;
40 namespace System.ComponentModel
42 public class EnumConverter : TypeConverter
45 private StandardValuesCollection stdValues;
47 public EnumConverter (Type type)
52 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
54 if (destinationType == typeof (InstanceDescriptor))
57 if (destinationType == typeof (Enum[]))
60 return base.CanConvertTo (context, destinationType);
63 public override object ConvertTo (ITypeDescriptorContext context,
68 if (destinationType == typeof (string) && value != null) {
69 // we need to be able to convert enum names,
70 // integral values and other enum fields
71 if (value is IConvertible) {
72 Type underlyingType = Enum.GetUnderlyingType (type);
73 if (underlyingType != value.GetType ()) {
74 value = ((IConvertible) value).ToType (
75 underlyingType, culture);
79 if (!IsFlags && !IsValid (context, value))
80 throw CreateValueNotValidException (value);
82 return Enum.Format (type, value, "G");
83 } else if (destinationType == typeof (InstanceDescriptor) && value != null) {
84 string enumString = ConvertToString (context, culture, value);
85 if (IsFlags && enumString.IndexOf (",") != -1) {
86 if (value is IConvertible) {
87 Type underlyingType = Enum.GetUnderlyingType (type);
88 object enumValue = ((IConvertible)value).ToType (underlyingType, culture);
89 MethodInfo toObjectMethod = typeof (Enum).GetMethod ("ToObject", new Type[] { typeof (Type),
91 return new InstanceDescriptor (toObjectMethod, new object[] { type, enumValue });
94 FieldInfo f = type.GetField (enumString);
96 return new InstanceDescriptor (f, null);
98 } else if (destinationType == typeof (Enum[]) && value != null) {
100 return new Enum[] { (Enum) Enum.ToObject (type, value) };
102 long valueLong = Convert.ToInt64 (
103 (Enum) value, culture);
104 Array enumValArray = Enum.GetValues (type);
105 long[] enumValues = new long[enumValArray.Length];
106 for (int i=0; i < enumValArray.Length; i++)
107 enumValues[i] = Convert.ToInt64 (enumValArray.GetValue (i));
109 ArrayList enums = new ArrayList ();
110 bool interrupt = false;
112 // interrupt the loop unless we find a match to avoid
113 // looping indefinitely
115 foreach (long val in enumValues) {
116 if ((val != 0 && ((val & valueLong) == val)) || val == valueLong) {
117 enums.Add (Enum.ToObject (type, val));
122 if (valueLong == 0) // nothing left to do
126 // add item for remainder
128 enums.Add (Enum.ToObject (type, valueLong));
130 return enums.ToArray (typeof(Enum));
134 return base.ConvertTo (context, culture, value, destinationType);
137 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
139 if (sourceType == typeof (string))
142 if (sourceType == typeof (Enum[]))
145 return base.CanConvertFrom (context, sourceType);
148 public override object ConvertFrom (ITypeDescriptorContext context,
152 if (value is string) {
153 string name = value as string;
155 if (name.IndexOf (',') == -1)
156 return Enum.Parse (type, name, true);
159 string [] names = name.Split (',');
160 foreach (string n in names) {
161 Enum e = (Enum) Enum.Parse (type, n, true);
162 val |= Convert.ToInt64 (e, culture);
164 return Enum.ToObject (type, val);
165 } catch (Exception ex) {
166 throw new FormatException (name + " is " +
167 "not a valid value for " +
170 } else if (value is Enum[]) {
172 foreach (Enum e in (Enum[])value)
173 val |= Convert.ToInt64 (e, culture);
174 return Enum.ToObject (type, val);
177 return base.ConvertFrom (context, culture, value);
180 public override bool IsValid (ITypeDescriptorContext context, object value)
182 return Enum.IsDefined (type, value);
185 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
190 public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
195 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
197 if (stdValues == null) {
198 Array values = Enum.GetValues (type);
200 stdValues = new StandardValuesCollection (values);
205 protected virtual IComparer Comparer {
206 get { return new EnumConverter.EnumComparer (); }
209 protected Type EnumType {
213 protected TypeConverter.StandardValuesCollection Values {
214 get { return stdValues; }
215 set { stdValues = value; }
218 ArgumentException CreateValueNotValidException (object value)
220 string msg = string.Format (CultureInfo.InvariantCulture,
221 "The value '{0}' is not a valid value for the " +
222 "enum '{1}'", value, type.Name);
223 return new ArgumentException (msg);
228 return (type.IsDefined (typeof (FlagsAttribute),
233 private class EnumComparer : IComparer
235 int IComparer.Compare (object compareObject1, object compareObject2)
237 string CompareString1 = (compareObject1 as string);
238 string CompareString2 = (compareObject2 as string);
239 if ((CompareString1 == null) || (CompareString2 == null))
240 return Collections.Comparer.Default.Compare (compareObject1, compareObject2);
241 return CultureInfo.InvariantCulture.CompareInfo.Compare (CompareString1, CompareString2);