mscorlib(4.0.0.0) API with additions
Enum.cs
2 using System.Reflection;
5 using System.Security;
6 using System.Text;
7 
8 namespace System
9 {
11  [Serializable]
12  [ComVisible(true)]
13  [__DynamicallyInvokable]
14  public abstract class Enum : IComparable, IFormattable, IConvertible
15  {
16  private enum ParseFailureKind
17  {
18  None,
19  Argument,
20  ArgumentNull,
21  ArgumentWithParameter,
22  UnhandledException
23  }
24 
25  private struct EnumResult
26  {
27  internal object parsedEnum;
28 
29  internal bool canThrow;
30 
31  internal ParseFailureKind m_failure;
32 
33  internal string m_failureMessageID;
34 
35  internal string m_failureParameter;
36 
37  internal object m_failureMessageFormatArgument;
38 
39  internal Exception m_innerException;
40 
41  internal void Init(bool canMethodThrow)
42  {
43  parsedEnum = 0;
44  canThrow = canMethodThrow;
45  }
46 
47  internal void SetFailure(Exception unhandledException)
48  {
49  m_failure = ParseFailureKind.UnhandledException;
50  m_innerException = unhandledException;
51  }
52 
53  internal void SetFailure(ParseFailureKind failure, string failureParameter)
54  {
55  m_failure = failure;
56  m_failureParameter = failureParameter;
57  if (canThrow)
58  {
59  throw GetEnumParseException();
60  }
61  }
62 
63  internal void SetFailure(ParseFailureKind failure, string failureMessageID, object failureMessageFormatArgument)
64  {
65  m_failure = failure;
66  m_failureMessageID = failureMessageID;
67  m_failureMessageFormatArgument = failureMessageFormatArgument;
68  if (canThrow)
69  {
70  throw GetEnumParseException();
71  }
72  }
73 
74  internal Exception GetEnumParseException()
75  {
76  switch (m_failure)
77  {
78  case ParseFailureKind.Argument:
79  return new ArgumentException(Environment.GetResourceString(m_failureMessageID));
80  case ParseFailureKind.ArgumentNull:
81  return new ArgumentNullException(m_failureParameter);
82  case ParseFailureKind.ArgumentWithParameter:
83  return new ArgumentException(Environment.GetResourceString(m_failureMessageID, m_failureMessageFormatArgument));
84  case ParseFailureKind.UnhandledException:
85  return m_innerException;
86  default:
87  return new ArgumentException(Environment.GetResourceString("Arg_EnumValueNotFound"));
88  }
89  }
90  }
91 
92  private class ValuesAndNames
93  {
94  public ulong[] Values;
95 
96  public string[] Names;
97 
98  public ValuesAndNames(ulong[] values, string[] names)
99  {
100  Values = values;
101  Names = names;
102  }
103  }
104 
105  private static readonly char[] enumSeperatorCharArray = new char[1]
106  {
107  ','
108  };
109 
110  private const string enumSeperator = ", ";
111 
112  [SecuritySafeCritical]
113  private static ValuesAndNames GetCachedValuesAndNames(RuntimeType enumType, bool getNames)
114  {
115  ValuesAndNames valuesAndNames = enumType.GenericCache as ValuesAndNames;
116  if (valuesAndNames == null || (getNames && valuesAndNames.Names == null))
117  {
118  ulong[] o = null;
119  string[] o2 = null;
120  GetEnumValuesAndNames(enumType.GetTypeHandleInternal(), JitHelpers.GetObjectHandleOnStack(ref o), JitHelpers.GetObjectHandleOnStack(ref o2), getNames);
121  valuesAndNames = (ValuesAndNames)(enumType.GenericCache = new ValuesAndNames(o, o2));
122  }
123  return valuesAndNames;
124  }
125 
126  private static string InternalFormattedHexString(object value)
127  {
128  switch (Convert.GetTypeCode(value))
129  {
130  case TypeCode.SByte:
131  return ((byte)(sbyte)value).ToString("X2", null);
132  case TypeCode.Byte:
133  return ((byte)value).ToString("X2", null);
134  case TypeCode.Boolean:
135  return Convert.ToByte((bool)value).ToString("X2", null);
136  case TypeCode.Int16:
137  return ((ushort)(short)value).ToString("X4", null);
138  case TypeCode.UInt16:
139  return ((ushort)value).ToString("X4", null);
140  case TypeCode.Char:
141  return ((ushort)(char)value).ToString("X4", null);
142  case TypeCode.UInt32:
143  return ((uint)value).ToString("X8", null);
144  case TypeCode.Int32:
145  return ((uint)(int)value).ToString("X8", null);
146  case TypeCode.UInt64:
147  return ((ulong)value).ToString("X16", null);
148  case TypeCode.Int64:
149  return ((ulong)(long)value).ToString("X16", null);
150  default:
151  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType"));
152  }
153  }
154 
155  private static string InternalFormat(RuntimeType eT, object value)
156  {
157  if (!eT.IsDefined(typeof(FlagsAttribute), inherit: false))
158  {
159  string name = GetName(eT, value);
160  if (name == null)
161  {
162  return value.ToString();
163  }
164  return name;
165  }
166  return InternalFlagsFormat(eT, value);
167  }
168 
169  private static string InternalFlagsFormat(RuntimeType eT, object value)
170  {
171  ulong num = ToUInt64(value);
172  ValuesAndNames cachedValuesAndNames = GetCachedValuesAndNames(eT, getNames: true);
173  string[] names = cachedValuesAndNames.Names;
174  ulong[] values = cachedValuesAndNames.Values;
175  int num2 = values.Length - 1;
176  StringBuilder stringBuilder = new StringBuilder();
177  bool flag = true;
178  ulong num3 = num;
179  while (num2 >= 0 && (num2 != 0 || values[num2] != 0L))
180  {
181  if ((num & values[num2]) == values[num2])
182  {
183  num -= values[num2];
184  if (!flag)
185  {
186  stringBuilder.Insert(0, ", ");
187  }
188  stringBuilder.Insert(0, names[num2]);
189  flag = false;
190  }
191  num2--;
192  }
193  if (num != 0L)
194  {
195  return value.ToString();
196  }
197  if (num3 == 0L)
198  {
199  if (values.Length != 0 && values[0] == 0L)
200  {
201  return names[0];
202  }
203  return "0";
204  }
205  return stringBuilder.ToString();
206  }
207 
208  internal static ulong ToUInt64(object value)
209  {
210  switch (Convert.GetTypeCode(value))
211  {
212  case TypeCode.SByte:
213  case TypeCode.Int16:
214  case TypeCode.Int32:
215  case TypeCode.Int64:
216  return (ulong)Convert.ToInt64(value, CultureInfo.InvariantCulture);
217  case TypeCode.Boolean:
218  case TypeCode.Char:
219  case TypeCode.Byte:
220  case TypeCode.UInt16:
221  case TypeCode.UInt32:
222  case TypeCode.UInt64:
224  default:
225  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType"));
226  }
227  }
228 
229  [MethodImpl(MethodImplOptions.InternalCall)]
230  [SecurityCritical]
231  private static extern int InternalCompareTo(object o1, object o2);
232 
233  [MethodImpl(MethodImplOptions.InternalCall)]
234  [SecuritySafeCritical]
235  internal static extern RuntimeType InternalGetUnderlyingType(RuntimeType enumType);
236 
237  [DllImport("QCall", CharSet = CharSet.Unicode)]
238  [SecurityCritical]
239  [SuppressUnmanagedCodeSecurity]
240  private static extern void GetEnumValuesAndNames(RuntimeTypeHandle enumType, ObjectHandleOnStack values, ObjectHandleOnStack names, bool getNames);
241 
242  [MethodImpl(MethodImplOptions.InternalCall)]
243  [SecurityCritical]
244  private static extern object InternalBoxEnum(RuntimeType enumType, long value);
245 
254  [__DynamicallyInvokable]
255  public static bool TryParse<TEnum>(string value, out TEnum result) where TEnum : struct
256  {
257  return TryParse(value, ignoreCase: false, out result);
258  }
259 
270  [__DynamicallyInvokable]
271  public static bool TryParse<TEnum>(string value, bool ignoreCase, out TEnum result) where TEnum : struct
272  {
273  result = default(TEnum);
274  EnumResult parseResult = default(EnumResult);
275  parseResult.Init(canMethodThrow: false);
276  bool result2;
277  if (result2 = TryParseEnum(typeof(TEnum), value, ignoreCase, ref parseResult))
278  {
279  result = (TEnum)parseResult.parsedEnum;
280  }
281  return result2;
282  }
283 
296  [ComVisible(true)]
297  [__DynamicallyInvokable]
298  public static object Parse(Type enumType, string value)
299  {
300  return Parse(enumType, value, ignoreCase: false);
301  }
302 
317  [ComVisible(true)]
318  [__DynamicallyInvokable]
319  public static object Parse(Type enumType, string value, bool ignoreCase)
320  {
321  EnumResult parseResult = default(EnumResult);
322  parseResult.Init(canMethodThrow: true);
323  if (TryParseEnum(enumType, value, ignoreCase, ref parseResult))
324  {
325  return parseResult.parsedEnum;
326  }
327  throw parseResult.GetEnumParseException();
328  }
329 
330  private static bool TryParseEnum(Type enumType, string value, bool ignoreCase, ref EnumResult parseResult)
331  {
332  if (enumType == null)
333  {
334  throw new ArgumentNullException("enumType");
335  }
336  RuntimeType runtimeType = enumType as RuntimeType;
337  if (runtimeType == null)
338  {
339  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
340  }
341  if (!enumType.IsEnum)
342  {
343  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
344  }
345  if (value == null)
346  {
347  parseResult.SetFailure(ParseFailureKind.ArgumentNull, "value");
348  return false;
349  }
350  value = value.Trim();
351  if (value.Length == 0)
352  {
353  parseResult.SetFailure(ParseFailureKind.Argument, "Arg_MustContainEnumInfo", null);
354  return false;
355  }
356  ulong num = 0uL;
357  if (char.IsDigit(value[0]) || value[0] == '-' || value[0] == '+')
358  {
359  Type underlyingType = GetUnderlyingType(enumType);
360  try
361  {
362  object value2 = Convert.ChangeType(value, underlyingType, CultureInfo.InvariantCulture);
363  parseResult.parsedEnum = ToObject(enumType, value2);
364  return true;
365  }
366  catch (FormatException)
367  {
368  }
369  catch (Exception failure)
370  {
371  if (parseResult.canThrow)
372  {
373  throw;
374  }
375  parseResult.SetFailure(failure);
376  return false;
377  }
378  }
379  string[] array = value.Split(enumSeperatorCharArray);
380  ValuesAndNames cachedValuesAndNames = GetCachedValuesAndNames(runtimeType, getNames: true);
381  string[] names = cachedValuesAndNames.Names;
382  ulong[] values = cachedValuesAndNames.Values;
383  for (int i = 0; i < array.Length; i++)
384  {
385  array[i] = array[i].Trim();
386  bool flag = false;
387  for (int j = 0; j < names.Length; j++)
388  {
389  if (ignoreCase)
390  {
391  if (string.Compare(names[j], array[i], StringComparison.OrdinalIgnoreCase) != 0)
392  {
393  continue;
394  }
395  }
396  else if (!names[j].Equals(array[i]))
397  {
398  continue;
399  }
400  ulong num2 = values[j];
401  num |= num2;
402  flag = true;
403  break;
404  }
405  if (!flag)
406  {
407  parseResult.SetFailure(ParseFailureKind.ArgumentWithParameter, "Arg_EnumValueNotFound", value);
408  return false;
409  }
410  }
411  try
412  {
413  parseResult.parsedEnum = ToObject(enumType, num);
414  return true;
415  }
416  catch (Exception failure2)
417  {
418  if (parseResult.canThrow)
419  {
420  throw;
421  }
422  parseResult.SetFailure(failure2);
423  return false;
424  }
425  }
426 
434  [ComVisible(true)]
435  [__DynamicallyInvokable]
436  public static Type GetUnderlyingType(Type enumType)
437  {
438  if (enumType == null)
439  {
440  throw new ArgumentNullException("enumType");
441  }
442  return enumType.GetEnumUnderlyingType();
443  }
444 
455  [ComVisible(true)]
456  [__DynamicallyInvokable]
457  public static Array GetValues(Type enumType)
458  {
459  if (enumType == null)
460  {
461  throw new ArgumentNullException("enumType");
462  }
463  return enumType.GetEnumValues();
464  }
465 
466  internal static ulong[] InternalGetValues(RuntimeType enumType)
467  {
468  return GetCachedValuesAndNames(enumType, getNames: false).Values;
469  }
470 
480  [ComVisible(true)]
481  [__DynamicallyInvokable]
482  public static string GetName(Type enumType, object value)
483  {
484  if (enumType == null)
485  {
486  throw new ArgumentNullException("enumType");
487  }
488  return enumType.GetEnumName(value);
489  }
490 
498  [ComVisible(true)]
499  [__DynamicallyInvokable]
500  public static string[] GetNames(Type enumType)
501  {
502  if (enumType == null)
503  {
504  throw new ArgumentNullException("enumType");
505  }
506  return enumType.GetEnumNames();
507  }
508 
509  internal static string[] InternalGetNames(RuntimeType enumType)
510  {
511  return GetCachedValuesAndNames(enumType, getNames: true).Names;
512  }
513 
523  [ComVisible(true)]
524  [__DynamicallyInvokable]
525  public static object ToObject(Type enumType, object value)
526  {
527  if (value == null)
528  {
529  throw new ArgumentNullException("value");
530  }
531  TypeCode typeCode = Convert.GetTypeCode(value);
532  if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8 && (typeCode == TypeCode.Boolean || typeCode == TypeCode.Char))
533  {
534  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnumBaseTypeOrEnum"), "value");
535  }
536  switch (typeCode)
537  {
538  case TypeCode.Int32:
539  return ToObject(enumType, (int)value);
540  case TypeCode.SByte:
541  return ToObject(enumType, (sbyte)value);
542  case TypeCode.Int16:
543  return ToObject(enumType, (short)value);
544  case TypeCode.Int64:
545  return ToObject(enumType, (long)value);
546  case TypeCode.UInt32:
547  return ToObject(enumType, (uint)value);
548  case TypeCode.Byte:
549  return ToObject(enumType, (byte)value);
550  case TypeCode.UInt16:
551  return ToObject(enumType, (ushort)value);
552  case TypeCode.UInt64:
553  return ToObject(enumType, (ulong)value);
554  case TypeCode.Char:
555  return ToObject(enumType, (char)value);
556  case TypeCode.Boolean:
557  return ToObject(enumType, (bool)value);
558  default:
559  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnumBaseTypeOrEnum"), "value");
560  }
561  }
562 
575  [ComVisible(true)]
576  [__DynamicallyInvokable]
577  public static bool IsDefined(Type enumType, object value)
578  {
579  if (enumType == null)
580  {
581  throw new ArgumentNullException("enumType");
582  }
583  return enumType.IsEnumDefined(value);
584  }
585 
596  [ComVisible(true)]
597  [__DynamicallyInvokable]
598  public static string Format(Type enumType, object value, string format)
599  {
600  if (enumType == null)
601  {
602  throw new ArgumentNullException("enumType");
603  }
604  if (!enumType.IsEnum)
605  {
606  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
607  }
608  if (value == null)
609  {
610  throw new ArgumentNullException("value");
611  }
612  if (format == null)
613  {
614  throw new ArgumentNullException("format");
615  }
616  RuntimeType runtimeType = enumType as RuntimeType;
617  if (runtimeType == null)
618  {
619  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
620  }
621  Type type = value.GetType();
622  Type underlyingType = GetUnderlyingType(enumType);
623  if (type.IsEnum)
624  {
625  Type underlyingType2 = GetUnderlyingType(type);
626  if (!type.IsEquivalentTo(enumType))
627  {
628  throw new ArgumentException(Environment.GetResourceString("Arg_EnumAndObjectMustBeSameType", type.ToString(), enumType.ToString()));
629  }
630  type = underlyingType2;
631  value = ((Enum)value).GetValue();
632  }
633  else if (type != underlyingType)
634  {
635  throw new ArgumentException(Environment.GetResourceString("Arg_EnumFormatUnderlyingTypeAndObjectMustBeSameType", type.ToString(), underlyingType.ToString()));
636  }
637  if (format.Length != 1)
638  {
639  throw new FormatException(Environment.GetResourceString("Format_InvalidEnumFormatSpecification"));
640  }
641  switch (format[0])
642  {
643  case 'D':
644  case 'd':
645  return value.ToString();
646  case 'X':
647  case 'x':
648  return InternalFormattedHexString(value);
649  case 'G':
650  case 'g':
651  return InternalFormat(runtimeType, value);
652  case 'F':
653  case 'f':
654  return InternalFlagsFormat(runtimeType, value);
655  default:
656  throw new FormatException(Environment.GetResourceString("Format_InvalidEnumFormatSpecification"));
657  }
658  }
659 
660  [SecuritySafeCritical]
661  internal unsafe object GetValue()
662  {
663  fixed (IntPtr* ptr = (IntPtr*)(&JitHelpers.GetPinningHelper(this).m_data))
664  {
665  switch (InternalGetCorElementType())
666  {
667  case CorElementType.I1:
668  return *(sbyte*)ptr;
669  case CorElementType.U1:
670  return *(byte*)ptr;
671  case CorElementType.Boolean:
672  return *(byte*)ptr != 0;
673  case CorElementType.I2:
674  return *(short*)ptr;
675  case CorElementType.U2:
676  return *(ushort*)ptr;
677  case CorElementType.Char:
678  return (char)(*(ushort*)ptr);
679  case CorElementType.I4:
680  return *(int*)ptr;
681  case CorElementType.U4:
682  return *(uint*)ptr;
683  case CorElementType.R4:
684  return *(float*)ptr;
685  case CorElementType.I8:
686  return *(long*)ptr;
687  case CorElementType.U8:
688  return (ulong)(*(long*)ptr);
689  case CorElementType.R8:
690  return *(double*)ptr;
691  case CorElementType.I:
692  return *ptr;
693  case CorElementType.U:
694  return (UIntPtr)(void*)(long)(*ptr);
695  default:
696  return null;
697  }
698  }
699  }
700 
701  [MethodImpl(MethodImplOptions.InternalCall)]
702  [SecurityCritical]
703  private extern bool InternalHasFlag(Enum flags);
704 
705  [MethodImpl(MethodImplOptions.InternalCall)]
706  [SecuritySafeCritical]
707  private extern CorElementType InternalGetCorElementType();
708 
713  [MethodImpl(MethodImplOptions.InternalCall)]
714  [SecuritySafeCritical]
715  [__DynamicallyInvokable]
716  public override extern bool Equals(object obj);
717 
720  [SecuritySafeCritical]
721  [__DynamicallyInvokable]
722  public unsafe override int GetHashCode()
723  {
724  fixed (IntPtr* ptr = (IntPtr*)(&JitHelpers.GetPinningHelper(this).m_data))
725  {
726  switch (InternalGetCorElementType())
727  {
728  case CorElementType.I1:
729  return ((sbyte*)ptr)->GetHashCode();
730  case CorElementType.U1:
731  return ((byte*)ptr)->GetHashCode();
732  case CorElementType.Boolean:
733  return ((bool*)ptr)->GetHashCode();
734  case CorElementType.I2:
735  return ((short*)ptr)->GetHashCode();
736  case CorElementType.U2:
737  return ((ushort*)ptr)->GetHashCode();
738  case CorElementType.Char:
739  return ((char*)ptr)->GetHashCode();
740  case CorElementType.I4:
741  return ((int*)ptr)->GetHashCode();
742  case CorElementType.U4:
743  return ((uint*)ptr)->GetHashCode();
744  case CorElementType.R4:
745  return ((float*)ptr)->GetHashCode();
746  case CorElementType.I8:
747  return ((long*)ptr)->GetHashCode();
748  case CorElementType.U8:
749  return ((ulong*)ptr)->GetHashCode();
750  case CorElementType.R8:
751  return ((double*)ptr)->GetHashCode();
752  case CorElementType.I:
753  return ptr->GetHashCode();
754  case CorElementType.U:
755  return ((UIntPtr*)ptr)->GetHashCode();
756  default:
757  return 0;
758  }
759  }
760  }
761 
764  [__DynamicallyInvokable]
765  public override string ToString()
766  {
767  return InternalFormat((RuntimeType)GetType(), GetValue());
768  }
769 
778  [Obsolete("The provider argument is not used. Please use ToString(String).")]
779  public string ToString(string format, IFormatProvider provider)
780  {
781  return ToString(format);
782  }
783 
791  [SecuritySafeCritical]
792  [__DynamicallyInvokable]
793  public int CompareTo(object target)
794  {
795  if (this == null)
796  {
797  throw new NullReferenceException();
798  }
799  int num = InternalCompareTo(this, target);
800  if (num < 2)
801  {
802  return num;
803  }
804  if (num == 2)
805  {
806  Type type = GetType();
807  Type type2 = target.GetType();
808  throw new ArgumentException(Environment.GetResourceString("Arg_EnumAndObjectMustBeSameType", type2.ToString(), type.ToString()));
809  }
810  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType"));
811  }
812 
820  [__DynamicallyInvokable]
821  public string ToString(string format)
822  {
823  if (format == null || format.Length == 0)
824  {
825  format = "G";
826  }
827  if (string.Compare(format, "G", StringComparison.OrdinalIgnoreCase) == 0)
828  {
829  return ToString();
830  }
831  if (string.Compare(format, "D", StringComparison.OrdinalIgnoreCase) == 0)
832  {
833  return GetValue().ToString();
834  }
835  if (string.Compare(format, "X", StringComparison.OrdinalIgnoreCase) == 0)
836  {
837  return InternalFormattedHexString(GetValue());
838  }
839  if (string.Compare(format, "F", StringComparison.OrdinalIgnoreCase) == 0)
840  {
841  return InternalFlagsFormat((RuntimeType)GetType(), GetValue());
842  }
843  throw new FormatException(Environment.GetResourceString("Format_InvalidEnumFormatSpecification"));
844  }
845 
849  [Obsolete("The provider argument is not used. Please use ToString().")]
850  public string ToString(IFormatProvider provider)
851  {
852  return ToString();
853  }
854 
861  [SecuritySafeCritical]
862  [__DynamicallyInvokable]
863  public bool HasFlag(Enum flag)
864  {
865  if (flag == null)
866  {
867  throw new ArgumentNullException("flag");
868  }
869  if (!GetType().IsEquivalentTo(flag.GetType()))
870  {
871  throw new ArgumentException(Environment.GetResourceString("Argument_EnumTypeDoesNotMatch", flag.GetType(), GetType()));
872  }
873  return InternalHasFlag(flag);
874  }
875 
880  {
881  Type type = GetType();
882  Type underlyingType = GetUnderlyingType(type);
883  if (underlyingType == typeof(int))
884  {
885  return TypeCode.Int32;
886  }
887  if (underlyingType == typeof(sbyte))
888  {
889  return TypeCode.SByte;
890  }
891  if (underlyingType == typeof(short))
892  {
893  return TypeCode.Int16;
894  }
895  if (underlyingType == typeof(long))
896  {
897  return TypeCode.Int64;
898  }
899  if (underlyingType == typeof(uint))
900  {
901  return TypeCode.UInt32;
902  }
903  if (underlyingType == typeof(byte))
904  {
905  return TypeCode.Byte;
906  }
907  if (underlyingType == typeof(ushort))
908  {
909  return TypeCode.UInt16;
910  }
911  if (underlyingType == typeof(ulong))
912  {
913  return TypeCode.UInt64;
914  }
915  if (underlyingType == typeof(bool))
916  {
917  return TypeCode.Boolean;
918  }
919  if (underlyingType == typeof(char))
920  {
921  return TypeCode.Char;
922  }
923  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType"));
924  }
925 
930  [__DynamicallyInvokable]
931  bool IConvertible.ToBoolean(IFormatProvider provider)
932  {
933  return Convert.ToBoolean(GetValue(), CultureInfo.CurrentCulture);
934  }
935 
940  [__DynamicallyInvokable]
941  char IConvertible.ToChar(IFormatProvider provider)
942  {
943  return Convert.ToChar(GetValue(), CultureInfo.CurrentCulture);
944  }
945 
949  [__DynamicallyInvokable]
950  sbyte IConvertible.ToSByte(IFormatProvider provider)
951  {
952  return Convert.ToSByte(GetValue(), CultureInfo.CurrentCulture);
953  }
954 
958  [__DynamicallyInvokable]
959  byte IConvertible.ToByte(IFormatProvider provider)
960  {
961  return Convert.ToByte(GetValue(), CultureInfo.CurrentCulture);
962  }
963 
967  [__DynamicallyInvokable]
968  short IConvertible.ToInt16(IFormatProvider provider)
969  {
970  return Convert.ToInt16(GetValue(), CultureInfo.CurrentCulture);
971  }
972 
976  [__DynamicallyInvokable]
977  ushort IConvertible.ToUInt16(IFormatProvider provider)
978  {
979  return Convert.ToUInt16(GetValue(), CultureInfo.CurrentCulture);
980  }
981 
985  [__DynamicallyInvokable]
986  int IConvertible.ToInt32(IFormatProvider provider)
987  {
988  return Convert.ToInt32(GetValue(), CultureInfo.CurrentCulture);
989  }
990 
994  [__DynamicallyInvokable]
995  uint IConvertible.ToUInt32(IFormatProvider provider)
996  {
997  return Convert.ToUInt32(GetValue(), CultureInfo.CurrentCulture);
998  }
999 
1003  [__DynamicallyInvokable]
1004  long IConvertible.ToInt64(IFormatProvider provider)
1005  {
1006  return Convert.ToInt64(GetValue(), CultureInfo.CurrentCulture);
1007  }
1008 
1012  [__DynamicallyInvokable]
1013  ulong IConvertible.ToUInt64(IFormatProvider provider)
1014  {
1015  return Convert.ToUInt64(GetValue(), CultureInfo.CurrentCulture);
1016  }
1017 
1022  [__DynamicallyInvokable]
1023  float IConvertible.ToSingle(IFormatProvider provider)
1024  {
1025  return Convert.ToSingle(GetValue(), CultureInfo.CurrentCulture);
1026  }
1027 
1032  [__DynamicallyInvokable]
1033  double IConvertible.ToDouble(IFormatProvider provider)
1034  {
1035  return Convert.ToDouble(GetValue(), CultureInfo.CurrentCulture);
1036  }
1037 
1042  [__DynamicallyInvokable]
1043  decimal IConvertible.ToDecimal(IFormatProvider provider)
1044  {
1045  return Convert.ToDecimal(GetValue(), CultureInfo.CurrentCulture);
1046  }
1047 
1052  [__DynamicallyInvokable]
1053  DateTime IConvertible.ToDateTime(IFormatProvider provider)
1054  {
1055  throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Enum", "DateTime"));
1056  }
1057 
1062  [__DynamicallyInvokable]
1063  object IConvertible.ToType(Type type, IFormatProvider provider)
1064  {
1065  return Convert.DefaultToType(this, type, provider);
1066  }
1067 
1076  [SecuritySafeCritical]
1077  [CLSCompliant(false)]
1078  [ComVisible(true)]
1079  public static object ToObject(Type enumType, sbyte value)
1080  {
1081  if (enumType == null)
1082  {
1083  throw new ArgumentNullException("enumType");
1084  }
1085  if (!enumType.IsEnum)
1086  {
1087  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
1088  }
1089  RuntimeType runtimeType = enumType as RuntimeType;
1090  if (runtimeType == null)
1091  {
1092  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
1093  }
1094  return InternalBoxEnum(runtimeType, value);
1095  }
1096 
1105  [SecuritySafeCritical]
1106  [ComVisible(true)]
1107  public static object ToObject(Type enumType, short value)
1108  {
1109  if (enumType == null)
1110  {
1111  throw new ArgumentNullException("enumType");
1112  }
1113  if (!enumType.IsEnum)
1114  {
1115  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
1116  }
1117  RuntimeType runtimeType = enumType as RuntimeType;
1118  if (runtimeType == null)
1119  {
1120  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
1121  }
1122  return InternalBoxEnum(runtimeType, value);
1123  }
1124 
1133  [SecuritySafeCritical]
1134  [ComVisible(true)]
1135  public static object ToObject(Type enumType, int value)
1136  {
1137  if (enumType == null)
1138  {
1139  throw new ArgumentNullException("enumType");
1140  }
1141  if (!enumType.IsEnum)
1142  {
1143  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
1144  }
1145  RuntimeType runtimeType = enumType as RuntimeType;
1146  if (runtimeType == null)
1147  {
1148  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
1149  }
1150  return InternalBoxEnum(runtimeType, value);
1151  }
1152 
1161  [SecuritySafeCritical]
1162  [ComVisible(true)]
1163  public static object ToObject(Type enumType, byte value)
1164  {
1165  if (enumType == null)
1166  {
1167  throw new ArgumentNullException("enumType");
1168  }
1169  if (!enumType.IsEnum)
1170  {
1171  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
1172  }
1173  RuntimeType runtimeType = enumType as RuntimeType;
1174  if (runtimeType == null)
1175  {
1176  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
1177  }
1178  return InternalBoxEnum(runtimeType, value);
1179  }
1180 
1189  [SecuritySafeCritical]
1190  [CLSCompliant(false)]
1191  [ComVisible(true)]
1192  public static object ToObject(Type enumType, ushort value)
1193  {
1194  if (enumType == null)
1195  {
1196  throw new ArgumentNullException("enumType");
1197  }
1198  if (!enumType.IsEnum)
1199  {
1200  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
1201  }
1202  RuntimeType runtimeType = enumType as RuntimeType;
1203  if (runtimeType == null)
1204  {
1205  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
1206  }
1207  return InternalBoxEnum(runtimeType, value);
1208  }
1209 
1218  [SecuritySafeCritical]
1219  [CLSCompliant(false)]
1220  [ComVisible(true)]
1221  public static object ToObject(Type enumType, uint value)
1222  {
1223  if (enumType == null)
1224  {
1225  throw new ArgumentNullException("enumType");
1226  }
1227  if (!enumType.IsEnum)
1228  {
1229  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
1230  }
1231  RuntimeType runtimeType = enumType as RuntimeType;
1232  if (runtimeType == null)
1233  {
1234  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
1235  }
1236  return InternalBoxEnum(runtimeType, value);
1237  }
1238 
1247  [SecuritySafeCritical]
1248  [ComVisible(true)]
1249  public static object ToObject(Type enumType, long value)
1250  {
1251  if (enumType == null)
1252  {
1253  throw new ArgumentNullException("enumType");
1254  }
1255  if (!enumType.IsEnum)
1256  {
1257  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
1258  }
1259  RuntimeType runtimeType = enumType as RuntimeType;
1260  if (runtimeType == null)
1261  {
1262  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
1263  }
1264  return InternalBoxEnum(runtimeType, value);
1265  }
1266 
1275  [SecuritySafeCritical]
1276  [CLSCompliant(false)]
1277  [ComVisible(true)]
1278  public static object ToObject(Type enumType, ulong value)
1279  {
1280  if (enumType == null)
1281  {
1282  throw new ArgumentNullException("enumType");
1283  }
1284  if (!enumType.IsEnum)
1285  {
1286  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
1287  }
1288  RuntimeType runtimeType = enumType as RuntimeType;
1289  if (runtimeType == null)
1290  {
1291  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
1292  }
1293  return InternalBoxEnum(runtimeType, (long)value);
1294  }
1295 
1296  [SecuritySafeCritical]
1297  private static object ToObject(Type enumType, char value)
1298  {
1299  if (enumType == null)
1300  {
1301  throw new ArgumentNullException("enumType");
1302  }
1303  if (!enumType.IsEnum)
1304  {
1305  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
1306  }
1307  RuntimeType runtimeType = enumType as RuntimeType;
1308  if (runtimeType == null)
1309  {
1310  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
1311  }
1312  return InternalBoxEnum(runtimeType, value);
1313  }
1314 
1315  [SecuritySafeCritical]
1316  private static object ToObject(Type enumType, bool value)
1317  {
1318  if (enumType == null)
1319  {
1320  throw new ArgumentNullException("enumType");
1321  }
1322  if (!enumType.IsEnum)
1323  {
1324  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
1325  }
1326  RuntimeType runtimeType = enumType as RuntimeType;
1327  if (runtimeType == null)
1328  {
1329  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
1330  }
1331  return InternalBoxEnum(runtimeType, value ? 1 : 0);
1332  }
1333 
1335  [__DynamicallyInvokable]
1336  protected Enum()
1337  {
1338  }
1339  }
1340 }
static object ToObject(Type enumType, int value)
Converts the specified 32-bit signed integer to an enumeration member.
Definition: Enum.cs:1135
Converts a base data type to another base data type.
Definition: Convert.cs:10
A platform-specific type that is used to represent a pointer or a handle.
Definition: UIntPtr.cs:14
static string GetName(Type enumType, object value)
Retrieves the name of the constant in the specified enumeration that has the specified value.
Definition: Enum.cs:482
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
static long ToInt64(object value)
Converts the value of the specified object to a 64-bit signed integer.
Definition: Convert.cs:2506
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
The exception that is thrown when there is an attempt to dereference a null object reference.
bool HasFlag(Enum flag)
Determines whether one or more bit fields are set in the current instance.
Definition: Enum.cs:863
string ToString(string format, IFormatProvider provider)
This method overload is obsolete; use M:System.Enum.ToString(System.String).
Definition: Enum.cs:779
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
string ToString(IFormatProvider provider)
This method overload is obsolete; use M:System.Enum.ToString.
Definition: Enum.cs:850
virtual bool IsEquivalentTo(Type other)
Determines whether two COM types have the same identity and are eligible for type equivalence.
Definition: Type.cs:2749
unsafe StringBuilder Insert(int index, string value, int count)
Inserts one or more copies of a specified string into this instance at the specified character positi...
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
bool ToBoolean(IFormatProvider provider)
Converts the value of this instance to an equivalent Boolean value using the specified culture-specif...
TypeCode
Specifies the type of an object.
Definition: TypeCode.cs:9
string ToString(string format)
Converts the value of this instance to its equivalent string representation using the specified forma...
Definition: Enum.cs:821
Definition: __Canon.cs:3
static object ToObject(Type enumType, byte value)
Converts the specified 8-bit unsigned integer to an enumeration member.
Definition: Enum.cs:1163
override string ToString()
Returns a String representing the name of the current Type.
Definition: Type.cs:2788
override string ToString()
Converts the value of this instance to its equivalent string representation.
Definition: Enum.cs:765
static object ToObject(Type enumType, ushort value)
Converts the specified 16-bit unsigned integer value to an enumeration member.
Definition: Enum.cs:1192
Represents a type using an internal metadata token.
static bool TryParse< TEnum >(string value, out TEnum result)
Converts the string representation of the name or numeric value of one or more enumerated constants t...
Definition: Enum.cs:255
Provides a mechanism for retrieving an object to control formatting.
static object Parse(Type enumType, string value, bool ignoreCase)
Converts the string representation of the name or numeric value of one or more enumerated constants t...
Definition: Enum.cs:319
Enum()
Initializes a new instance of the T:System.Enum class.
Definition: Enum.cs:1336
static string [] GetNames(Type enumType)
Retrieves an array of the names of the constants in a specified enumeration.
Definition: Enum.cs:500
static object ToObject(Type enumType, long value)
Converts the specified 64-bit signed integer to an enumeration member.
Definition: Enum.cs:1249
Indicates that an enumeration can be treated as a bit field; that is, a set of flags.
virtual bool IsEnum
Gets a value indicating whether the current T:System.Type represents an enumeration.
Definition: Type.cs:484
Defines a generalized type-specific comparison method that a value type or class implements to order ...
Definition: IComparable.cs:8
A type representing a date and time value.
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
static object ToObject(Type enumType, uint value)
Converts the specified 32-bit unsigned integer value to an enumeration member.
Definition: Enum.cs:1221
virtual string [] GetEnumNames()
Returns the names of the members of the current enumeration type.
Definition: Type.cs:2467
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
TypeCode GetTypeCode()
Returns the type code of the underlying type of this enumeration member.
Definition: Enum.cs:879
static bool ToBoolean(object value)
Converts the value of a specified object to an equivalent Boolean value.
Definition: Convert.cs:457
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
A cast or conversion operation, such as (SampleType)obj in C::or CType(obj, SampleType) in Visual Bas...
The exception that is thrown when the format of an argument is invalid, or when a composite format st...
Provides the base class for enumerations.
Definition: Enum.cs:14
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
static object ToObject(Type enumType, sbyte value)
Converts the specified 8-bit signed integer value to an enumeration member.
Definition: Enum.cs:1079
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
override bool Equals(object obj)
Returns a value indicating whether this instance is equal to a specified object.
virtual string GetEnumName(object value)
Returns the name of the constant that has the specified value, for the current enumeration type.
Definition: Type.cs:2611
MethodImplOptions
Defines the details of how a method is implemented.
CharSet
Dictates which character set marshaled strings should use.
Definition: CharSet.cs:7
static TypeCode GetTypeCode(object value)
Returns the T:System.TypeCode for the specified object.
Definition: Convert.cs:115
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
Definition: CultureInfo.cs:120
The exception that is thrown when one of the arguments provided to a method is not valid.
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
virtual Type GetEnumUnderlyingType()
Returns the underlying type of the current enumeration type.
Definition: Type.cs:2536
static byte ToByte(object value)
Converts the value of the specified object to an 8-bit unsigned integer.
Definition: Convert.cs:1186
virtual bool IsEnumDefined(object value)
Returns a value that indicates whether the specified value exists in the current enumeration type.
Definition: Type.cs:2559
static object ToObject(Type enumType, short value)
Converts the specified 16-bit signed integer to an enumeration member.
Definition: Enum.cs:1107
Specifies that the class can be serialized.
The exception that is thrown when a method call is invalid for the object's current state.
static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Gets the T:System.Type with the specified name, specifying whether to throw an exception if the type ...
Definition: Type.cs:853
static Type GetUnderlyingType(Type enumType)
Returns the underlying type of the specified enumeration.
Definition: Enum.cs:436
static ulong ToUInt64(object value)
Converts the value of the specified object to a 64-bit unsigned integer.
Definition: Convert.cs:2727
int CompareTo(object target)
Compares this instance to a specified object and returns an indication of their relative values.
Definition: Enum.cs:793
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
static object ToObject(Type enumType, ulong value)
Converts the specified 64-bit unsigned integer value to an enumeration member.
Definition: Enum.cs:1278
Defines methods that convert the value of the implementing reference or value type to a common langua...
Definition: IConvertible.cs:9
Provides functionality to format the value of an object into a string representation.
Definition: IFormattable.cs:8
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
unsafe override int GetHashCode()
Returns the hash code for the value of this instance.
Definition: Enum.cs:722
virtual Array GetEnumValues()
Returns an array of the values of the constants in the current enumeration type.
Definition: Type.cs:2480