using System; using System.Collections; using System.ComponentModel.Design.Serialization; using System.Globalization; using System.Reflection; namespace System.ComponentModel { /// Provides a type converter to convert objects to and from various other representations. // Token: 0x0200007C RID: 124 public class EnumConverter : TypeConverter { /// Initializes a new instance of the class for the given type. /// A that represents the type of enumeration to associate with this enumeration converter. // Token: 0x0600045D RID: 1117 RVA: 0x0000CAD0 File Offset: 0x0000ACD0 public EnumConverter(Type type) { this.type = type; } /// Gets a value indicating whether this converter can convert an object to the given destination type using the context. /// true if this converter can perform the conversion; otherwise, false. /// An that provides a format context. /// A that represents the type you wish to convert to. // Token: 0x0600045E RID: 1118 RVA: 0x0000CAE0 File Offset: 0x0000ACE0 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { return destinationType == typeof(global::System.ComponentModel.Design.Serialization.InstanceDescriptor) || destinationType == typeof(Enum[]) || base.CanConvertTo(context, destinationType); } /// Converts the given value object to the specified destination type. /// An that represents the converted . /// An that provides a format context. /// An optional . If not supplied, the current culture is assumed. /// The to convert. /// The to convert the value to. /// /// is null. /// /// is not a valid value for the enumeration. /// The conversion cannot be performed. // Token: 0x0600045F RID: 1119 RVA: 0x0000CB1C File Offset: 0x0000AD1C public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType != typeof(string) || value == null) { if (destinationType == typeof(global::System.ComponentModel.Design.Serialization.InstanceDescriptor) && value != null) { string text = base.ConvertToString(context, culture, value); if (this.IsFlags && text.IndexOf(",") != -1) { if (value is IConvertible) { Type underlyingType = Enum.GetUnderlyingType(this.type); object obj = ((IConvertible)value).ToType(underlyingType, culture); MethodInfo method = typeof(Enum).GetMethod("ToObject", new Type[] { typeof(Type), underlyingType }); return new global::System.ComponentModel.Design.Serialization.InstanceDescriptor(method, new object[] { this.type, obj }); } } else { FieldInfo field = this.type.GetField(text); if (field != null) { return new global::System.ComponentModel.Design.Serialization.InstanceDescriptor(field, null); } } } else if (destinationType == typeof(Enum[]) && value != null) { if (!this.IsFlags) { return new Enum[] { (Enum)Enum.ToObject(this.type, value) }; } long num = Convert.ToInt64((Enum)value, culture); Array values = Enum.GetValues(this.type); long[] array = new long[values.Length]; for (int i = 0; i < values.Length; i++) { array[i] = Convert.ToInt64(values.GetValue(i)); } ArrayList arrayList = new ArrayList(); bool flag = false; while (!flag) { flag = true; foreach (long num2 in array) { if ((num2 != 0L && (num2 & num) == num2) || num2 == num) { arrayList.Add(Enum.ToObject(this.type, num2)); num &= ~num2; flag = false; } } if (num == 0L) { flag = true; } } if (num != 0L) { arrayList.Add(Enum.ToObject(this.type, num)); } return arrayList.ToArray(typeof(Enum)); } return base.ConvertTo(context, culture, value, destinationType); } if (value is IConvertible) { Type underlyingType2 = Enum.GetUnderlyingType(this.type); if (underlyingType2 != value.GetType()) { value = ((IConvertible)value).ToType(underlyingType2, culture); } } if (!this.IsFlags && !this.IsValid(context, value)) { throw this.CreateValueNotValidException(value); } return Enum.Format(this.type, value, "G"); } /// Gets a value indicating whether this converter can convert an object in the given source type to an enumeration object using the specified context. /// true if this converter can perform the conversion; otherwise, false. /// An that provides a format context. /// A that represents the type you wish to convert from. // Token: 0x06000460 RID: 1120 RVA: 0x0000CDC8 File Offset: 0x0000AFC8 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(string) || sourceType == typeof(Enum[]) || base.CanConvertFrom(context, sourceType); } /// Converts the specified value object to an enumeration object. /// An that represents the converted . /// An that provides a format context. /// An optional . If not supplied, the current culture is assumed. /// The to convert. /// /// is not a valid value for the target type. /// The conversion cannot be performed. // Token: 0x06000461 RID: 1121 RVA: 0x0000CE04 File Offset: 0x0000B004 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string text = value as string; try { if (text.IndexOf(',') == -1) { return Enum.Parse(this.type, text, true); } long num = 0L; string[] array = text.Split(new char[] { ',' }); foreach (string text2 in array) { Enum @enum = (Enum)Enum.Parse(this.type, text2, true); num |= Convert.ToInt64(@enum, culture); } return Enum.ToObject(this.type, num); } catch (Exception ex) { throw new FormatException(text + " is not a valid value for " + this.type.Name, ex); } } if (value is Enum[]) { long num2 = 0L; foreach (Enum enum2 in (Enum[])value) { num2 |= Convert.ToInt64(enum2, culture); } return Enum.ToObject(this.type, num2); } return base.ConvertFrom(context, culture, value); } /// Gets a value indicating whether the given object value is valid for this type. /// true if the specified value is valid for this object; otherwise, false. /// An that provides a format context. /// The to test. // Token: 0x06000462 RID: 1122 RVA: 0x0000CF54 File Offset: 0x0000B154 public override bool IsValid(ITypeDescriptorContext context, object value) { return Enum.IsDefined(this.type, value); } /// Gets a value indicating whether this object supports a standard set of values that can be picked from a list using the specified context. /// true because should be called to find a common set of values the object supports. This method never returns false. /// An that provides a format context. // Token: 0x06000463 RID: 1123 RVA: 0x0000CF64 File Offset: 0x0000B164 public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return true; } /// Gets a value indicating whether the list of standard values returned from is an exclusive list using the specified context. /// true if the returned from is an exhaustive list of possible values; false if other values are possible. /// An that provides a format context. // Token: 0x06000464 RID: 1124 RVA: 0x0000CF68 File Offset: 0x0000B168 public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) { return !this.IsFlags; } /// Gets a collection of standard values for the data type this validator is designed for. /// A that holds a standard set of valid values, or null if the data type does not support a standard set of values. /// An that provides a format context. // Token: 0x06000465 RID: 1125 RVA: 0x0000CF74 File Offset: 0x0000B174 public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { if (this.stdValues == null) { Array values = Enum.GetValues(this.type); Array.Sort(values); this.stdValues = new TypeConverter.StandardValuesCollection(values); } return this.stdValues; } /// Gets an that can be used to sort the values of the enumeration. /// An for sorting the enumeration values. // Token: 0x17000127 RID: 295 // (get) Token: 0x06000466 RID: 1126 RVA: 0x0000CFB0 File Offset: 0x0000B1B0 protected virtual IComparer Comparer { get { return new EnumConverter.EnumComparer(); } } /// Specifies the type of the enumerator this converter is associated with. /// The type of the enumerator this converter is associated with. // Token: 0x17000128 RID: 296 // (get) Token: 0x06000467 RID: 1127 RVA: 0x0000CFB8 File Offset: 0x0000B1B8 protected Type EnumType { get { return this.type; } } /// Gets or sets a that specifies the possible values for the enumeration. /// A that specifies the possible values for the enumeration. // Token: 0x17000129 RID: 297 // (get) Token: 0x06000468 RID: 1128 RVA: 0x0000CFC0 File Offset: 0x0000B1C0 // (set) Token: 0x06000469 RID: 1129 RVA: 0x0000CFC8 File Offset: 0x0000B1C8 protected TypeConverter.StandardValuesCollection Values { get { return this.stdValues; } set { this.stdValues = value; } } // Token: 0x0600046A RID: 1130 RVA: 0x0000CFD4 File Offset: 0x0000B1D4 private ArgumentException CreateValueNotValidException(object value) { string text = string.Format(CultureInfo.InvariantCulture, "The value '{0}' is not a valid value for the enum '{1}'", new object[] { value, this.type.Name }); return new ArgumentException(text); } // Token: 0x1700012A RID: 298 // (get) Token: 0x0600046B RID: 1131 RVA: 0x0000D010 File Offset: 0x0000B210 private bool IsFlags { get { return this.type.IsDefined(typeof(FlagsAttribute), false); } } // Token: 0x04000172 RID: 370 private Type type; // Token: 0x04000173 RID: 371 private TypeConverter.StandardValuesCollection stdValues; // Token: 0x0200007D RID: 125 private class EnumComparer : IComparer { // Token: 0x0600046D RID: 1133 RVA: 0x0000D030 File Offset: 0x0000B230 int IComparer.Compare(object compareObject1, object compareObject2) { string text = compareObject1 as string; string text2 = compareObject2 as string; if (text == null || text2 == null) { return global::System.Collections.Comparer.Default.Compare(compareObject1, compareObject2); } return CultureInfo.InvariantCulture.CompareInfo.Compare(text, text2); } } } }