mscorlib(4.0.0.0) API with additions
EnumConverter.cs
1 using System.Collections;
5 using System.Reflection;
7 
8 namespace System.ComponentModel
9 {
11  [HostProtection(SecurityAction.LinkDemand, SharedState = true)]
13  {
14  private StandardValuesCollection values;
15 
16  private Type type;
17 
20  protected Type EnumType => type;
21 
25  {
26  get
27  {
28  return values;
29  }
30  set
31  {
32  values = value;
33  }
34  }
35 
38  protected virtual IComparer Comparer => System.InvariantComparer.Default;
39 
42  public EnumConverter(Type type)
43  {
44  this.type = type;
45  }
46 
52  public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
53  {
54  if (sourceType == typeof(string) || sourceType == typeof(Enum[]))
55  {
56  return true;
57  }
58  return base.CanConvertFrom(context, sourceType);
59  }
60 
66  public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
67  {
68  if (destinationType == typeof(InstanceDescriptor) || destinationType == typeof(Enum[]))
69  {
70  return true;
71  }
72  return base.CanConvertTo(context, destinationType);
73  }
74 
83  public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
84  {
85  if (value is string)
86  {
87  try
88  {
89  string text = (string)value;
90  if (text.IndexOf(',') != -1)
91  {
92  long num = 0L;
93  string[] array = text.Split(',');
94  string[] array2 = array;
95  foreach (string value2 in array2)
96  {
97  num |= Convert.ToInt64((Enum)Enum.Parse(type, value2, ignoreCase: true), culture);
98  }
99  return Enum.ToObject(type, num);
100  }
101  return Enum.Parse(type, text, ignoreCase: true);
102  }
103  catch (Exception innerException)
104  {
105  throw new FormatException(SR.GetString("ConvertInvalidPrimitive", (string)value, type.Name), innerException);
106  }
107  }
108  if (value is Enum[])
109  {
110  long num2 = 0L;
111  Enum[] array3 = (Enum[])value;
112  foreach (Enum value3 in array3)
113  {
114  num2 |= Convert.ToInt64(value3, culture);
115  }
116  return Enum.ToObject(type, num2);
117  }
118  return base.ConvertFrom(context, culture, value);
119  }
120 
132  public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
133  {
134  if (destinationType == null)
135  {
136  throw new ArgumentNullException("destinationType");
137  }
138  if (destinationType == typeof(string) && value != null)
139  {
140  Type underlyingType = Enum.GetUnderlyingType(type);
141  if (value is IConvertible && value.GetType() != underlyingType)
142  {
143  value = ((IConvertible)value).ToType(underlyingType, culture);
144  }
145  if (!type.IsDefined(typeof(FlagsAttribute), inherit: false) && !Enum.IsDefined(type, value))
146  {
147  throw new ArgumentException(SR.GetString("EnumConverterInvalidValue", value.ToString(), type.Name));
148  }
149  return Enum.Format(type, value, "G");
150  }
151  if (destinationType == typeof(InstanceDescriptor) && value != null)
152  {
153  string text = ConvertToInvariantString(context, value);
154  if (type.IsDefined(typeof(FlagsAttribute), inherit: false) && text.IndexOf(',') != -1)
155  {
156  Type underlyingType2 = Enum.GetUnderlyingType(type);
157  if (value is IConvertible)
158  {
159  object obj = ((IConvertible)value).ToType(underlyingType2, culture);
160  MethodInfo method = typeof(Enum).GetMethod("ToObject", new Type[2]
161  {
162  typeof(Type),
163  underlyingType2
164  });
165  if (method != null)
166  {
167  return new InstanceDescriptor(method, new object[2]
168  {
169  type,
170  obj
171  });
172  }
173  }
174  }
175  else
176  {
177  FieldInfo field = type.GetField(text);
178  if (field != null)
179  {
180  return new InstanceDescriptor(field, null);
181  }
182  }
183  }
184  if (destinationType == typeof(Enum[]) && value != null)
185  {
186  if (type.IsDefined(typeof(FlagsAttribute), inherit: false))
187  {
188  List<Enum> list = new List<Enum>();
189  Array array = Enum.GetValues(type);
190  long[] array2 = new long[array.Length];
191  for (int i = 0; i < array.Length; i++)
192  {
193  array2[i] = Convert.ToInt64((Enum)array.GetValue(i), culture);
194  }
195  long num = Convert.ToInt64((Enum)value, culture);
196  bool flag = true;
197  while (flag)
198  {
199  flag = false;
200  long[] array3 = array2;
201  foreach (long num2 in array3)
202  {
203  if ((num2 != 0L && (num2 & num) == num2) || num2 == num)
204  {
205  list.Add((Enum)Enum.ToObject(type, num2));
206  flag = true;
207  num &= ~num2;
208  break;
209  }
210  }
211  if (num == 0L)
212  {
213  break;
214  }
215  }
216  if (!flag && num != 0L)
217  {
218  list.Add((Enum)Enum.ToObject(type, num));
219  }
220  return list.ToArray();
221  }
222  return new Enum[1]
223  {
224  (Enum)Enum.ToObject(type, value)
225  };
226  }
227  return base.ConvertTo(context, culture, value, destinationType);
228  }
229 
234  {
235  if (values == null)
236  {
237  Type reflectionType = TypeDescriptor.GetReflectionType(type);
238  if (reflectionType == null)
239  {
240  reflectionType = type;
241  }
242  FieldInfo[] fields = reflectionType.GetFields(BindingFlags.Static | BindingFlags.Public);
243  ArrayList arrayList = null;
244  if (fields != null && fields.Length != 0)
245  {
246  arrayList = new ArrayList(fields.Length);
247  }
248  if (arrayList != null)
249  {
250  FieldInfo[] array = fields;
251  foreach (FieldInfo fieldInfo in array)
252  {
253  BrowsableAttribute browsableAttribute = null;
254  object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(BrowsableAttribute), inherit: false);
255  for (int j = 0; j < customAttributes.Length; j++)
256  {
257  Attribute attribute = (Attribute)customAttributes[j];
258  browsableAttribute = (attribute as BrowsableAttribute);
259  }
260  if (browsableAttribute == null || browsableAttribute.Browsable)
261  {
262  object obj = null;
263  try
264  {
265  if (fieldInfo.Name != null)
266  {
267  obj = Enum.Parse(type, fieldInfo.Name);
268  }
269  }
270  catch (ArgumentException)
271  {
272  }
273  if (obj != null)
274  {
275  arrayList.Add(obj);
276  }
277  }
278  }
279  IComparer comparer = Comparer;
280  if (comparer != null)
281  {
282  arrayList.Sort(comparer);
283  }
284  }
285  Array array2 = arrayList?.ToArray();
286  values = new StandardValuesCollection(array2);
287  }
288  return values;
289  }
290 
296  {
297  return !type.IsDefined(typeof(FlagsAttribute), inherit: false);
298  }
299 
305  {
306  return true;
307  }
308 
314  public override bool IsValid(ITypeDescriptorContext context, object value)
315  {
316  return Enum.IsDefined(type, value);
317  }
318  }
319 }
Provides the information necessary to create an instance of an object. This class cannot be inherited...
Converts a base data type to another base data type.
Definition: Convert.cs:10
FieldInfo [] GetFields()
Returns all the public fields of the current T:System.Type.
Definition: Type.cs:1695
abstract FieldInfo GetField(string name, BindingFlags bindingAttr)
Searches for the specified field, using the specified binding constraints.
static long ToInt64(object value)
Converts the value of the specified object to a 64-bit signed integer.
Definition: Convert.cs:2506
Compares two objects for equivalence, where string comparisons are case-sensitive.
Definition: Comparer.cs:11
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
Converts the specified value object to an enumeration object.
override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
Gets a value indicating whether this converter can convert an object in the given source type to an e...
override bool IsValid(ITypeDescriptorContext context, object value)
Gets a value indicating whether the given object value is valid for this type.
Type EnumType
Specifies the type of the enumerator this converter is associated with.
Discovers the attributes of a method and provides access to method metadata.
Definition: MethodInfo.cs:13
Specifies whether a property or event should be displayed in a Properties window.
Represents the base class for custom attributes.
Definition: Attribute.cs:15
Discovers the attributes of a field and provides access to field metadata.
Definition: FieldInfo.cs:15
BindingFlags
Specifies flags that control binding and the way in which the search for members and types is conduct...
Definition: BindingFlags.cs:10
Definition: __Canon.cs:3
abstract object [] GetCustomAttributes(bool inherit)
When overridden in a derived class, returns an array of all custom attributes applied to this member.
Provides contextual information about a component, such as its container and property descriptor.
Indicates that an enumeration can be treated as a bit field; that is, a set of flags.
override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
Gets a value indicating whether this converter can convert an object to the given destination type us...
static object Parse(Type enumType, string value)
Converts the string representation of the name or numeric value of one or more enumerated constants t...
Definition: Enum.cs:298
EnumConverter(Type type)
Initializes a new instance of the T:System.ComponentModel.EnumConverter class for the given type.
SecurityAction
Specifies the security actions that can be performed using declarative security.
abstract bool IsDefined(Type attributeType, bool inherit)
When overridden in a derived class, indicates whether one or more attributes of the specified type or...
static bool IsDefined(Type enumType, object value)
Returns an indication whether a constant with a specified value exists in a specified enumeration.
Definition: Enum.cs:577
Provides a type converter to convert T:System.Enum objects to and from various other representations.
override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
Gets a value indicating whether the list of standard values returned from M:System....
The exception that is thrown when the format of an argument is invalid, or when a composite format st...
override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
Converts the given value object to the specified destination type.
Provides the base class for enumerations.
Definition: Enum.cs:14
override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
Gets a collection of standard values for the data type this validator is designed for.
Exposes a method that compares two objects.
Definition: IComparer.cs:8
static Array GetValues(Type enumType)
Retrieves an array of the values of the constants in a specified enumeration.
Definition: Enum.cs:457
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
Provides information about the characteristics for a component, such as its attributes,...
abstract string Name
Gets the name of the current member.
Definition: MemberInfo.cs:27
bool Browsable
Gets a value indicating whether an object is browsable.
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
override bool GetStandardValuesSupported(ITypeDescriptorContext context)
Gets a value indicating whether this object supports a standard set of values that can be picked from...
The exception that is thrown when one of the arguments provided to a method is not valid.
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition: List.cs:14
static object ToObject(Type enumType, object value)
Converts the specified object with an integer value to an enumeration member.
Definition: Enum.cs:525
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
static Type GetReflectionType(Type type)
Returns a T:System.Type that can be used to perform reflection, given a class type.
virtual IComparer Comparer
Gets an T:System.Collections.IComparer that can be used to sort the values of the enumeration.
string ConvertToInvariantString(object value)
Converts the specified value to a culture-invariant string representation.
StandardValuesCollection Values
Gets or sets a T:System.ComponentModel.TypeConverter.StandardValuesCollection that specifies the poss...
static Type GetUnderlyingType(Type enumType)
Returns the underlying type of the specified enumeration.
Definition: Enum.cs:436
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
Defines methods that convert the value of the implementing reference or value type to a common langua...
Definition: IConvertible.cs:9
Provides a unified way of converting types of values to other types, as well as for accessing standar...
static string Format(Type enumType, object value, string format)
Converts the specified value of a specified enumerated type to its equivalent string representation a...
Definition: Enum.cs:598
virtual object [] ToArray()
Copies the elements of the T:System.Collections.ArrayList to a new T:System.Object array.
Definition: ArrayList.cs:3083
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14
virtual void Sort()
Sorts the elements in the entire T:System.Collections.ArrayList.
Definition: ArrayList.cs:3006