13 [__DynamicallyInvokable]
16 private enum ParseFailureKind
21 ArgumentWithParameter,
25 private struct EnumResult
27 internal object parsedEnum;
29 internal bool canThrow;
31 internal ParseFailureKind m_failure;
33 internal string m_failureMessageID;
35 internal string m_failureParameter;
37 internal object m_failureMessageFormatArgument;
41 internal void Init(
bool canMethodThrow)
44 canThrow = canMethodThrow;
47 internal void SetFailure(
Exception unhandledException)
49 m_failure = ParseFailureKind.UnhandledException;
50 m_innerException = unhandledException;
53 internal void SetFailure(ParseFailureKind failure,
string failureParameter)
56 m_failureParameter = failureParameter;
59 throw GetEnumParseException();
63 internal void SetFailure(ParseFailureKind failure,
string failureMessageID,
object failureMessageFormatArgument)
66 m_failureMessageID = failureMessageID;
67 m_failureMessageFormatArgument = failureMessageFormatArgument;
70 throw GetEnumParseException();
74 internal Exception GetEnumParseException()
78 case ParseFailureKind.Argument:
80 case ParseFailureKind.ArgumentNull:
82 case ParseFailureKind.ArgumentWithParameter:
84 case ParseFailureKind.UnhandledException:
85 return m_innerException;
92 private class ValuesAndNames
94 public ulong[] Values;
96 public string[] Names;
98 public ValuesAndNames(ulong[] values,
string[] names)
105 private static readonly
char[] enumSeperatorCharArray =
new char[1]
110 private const string enumSeperator =
", ";
112 [SecuritySafeCritical]
113 private static ValuesAndNames GetCachedValuesAndNames(RuntimeType enumType,
bool getNames)
115 ValuesAndNames valuesAndNames = enumType.GenericCache as ValuesAndNames;
116 if (valuesAndNames ==
null || (getNames && valuesAndNames.Names ==
null))
120 GetEnumValuesAndNames(enumType.GetTypeHandleInternal(), JitHelpers.GetObjectHandleOnStack(ref o), JitHelpers.GetObjectHandleOnStack(ref o2), getNames);
121 valuesAndNames = (ValuesAndNames)(enumType.GenericCache =
new ValuesAndNames(o, o2));
123 return valuesAndNames;
126 private static string InternalFormattedHexString(
object value)
131 return ((
byte)(sbyte)value).ToString(
"X2",
null);
133 return ((
byte)value).ToString(
"X2",
null);
137 return ((ushort)(
short)value).ToString(
"X4",
null);
139 return ((ushort)value).ToString(
"X4",
null);
141 return ((ushort)(
char)value).ToString(
"X4",
null);
143 return ((uint)value).ToString(
"X8",
null);
145 return ((uint)(
int)value).ToString(
"X8",
null);
147 return ((ulong)value).ToString(
"X16",
null);
149 return ((ulong)(
long)value).ToString(
"X16",
null);
155 private static string InternalFormat(RuntimeType eT,
object value)
159 string name =
GetName(eT, value);
162 return value.ToString();
166 return InternalFlagsFormat(eT, value);
169 private static string InternalFlagsFormat(RuntimeType eT,
object value)
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;
179 while (num2 >= 0 && (num2 != 0 || values[num2] != 0
L))
181 if ((num & values[num2]) == values[num2])
186 stringBuilder.
Insert(0,
", ");
188 stringBuilder.
Insert(0, names[num2]);
195 return value.ToString();
199 if (values.Length != 0 && values[0] == 0
L)
208 internal static ulong ToUInt64(
object value)
231 private static extern int InternalCompareTo(
object o1,
object o2);
234 [SecuritySafeCritical]
235 internal static extern RuntimeType InternalGetUnderlyingType(RuntimeType enumType);
239 [SuppressUnmanagedCodeSecurity]
240 private static extern void GetEnumValuesAndNames(
RuntimeTypeHandle enumType, ObjectHandleOnStack values, ObjectHandleOnStack names,
bool getNames);
244 private static extern object InternalBoxEnum(RuntimeType enumType,
long value);
254 [__DynamicallyInvokable]
255 public static bool TryParse<TEnum>(
string value, out TEnum result) where TEnum :
struct 257 return TryParse(value, ignoreCase:
false, out result);
270 [__DynamicallyInvokable]
271 public static bool TryParse<TEnum>(
string value,
bool ignoreCase, out TEnum result) where TEnum :
struct 273 result =
default(TEnum);
274 EnumResult parseResult =
default(EnumResult);
275 parseResult.Init(canMethodThrow:
false);
277 if (result2 = TryParseEnum(typeof(TEnum), value, ignoreCase, ref parseResult))
279 result = (TEnum)parseResult.parsedEnum;
297 [__DynamicallyInvokable]
300 return Parse(enumType, value, ignoreCase:
false);
318 [__DynamicallyInvokable]
319 public static object Parse(
Type enumType,
string value,
bool ignoreCase)
321 EnumResult parseResult =
default(EnumResult);
322 parseResult.Init(canMethodThrow:
true);
323 if (TryParseEnum(enumType, value, ignoreCase, ref parseResult))
325 return parseResult.parsedEnum;
327 throw parseResult.GetEnumParseException();
330 private static bool TryParseEnum(
Type enumType,
string value,
bool ignoreCase, ref EnumResult parseResult)
332 if (enumType ==
null)
336 RuntimeType runtimeType = enumType as RuntimeType;
337 if (runtimeType ==
null)
339 throw new ArgumentException(Environment.GetResourceString(
"Arg_MustBeType"),
"enumType");
343 throw new ArgumentException(Environment.GetResourceString(
"Arg_MustBeEnum"),
"enumType");
347 parseResult.SetFailure(ParseFailureKind.ArgumentNull,
"value");
350 value = value.Trim();
351 if (value.Length == 0)
353 parseResult.SetFailure(ParseFailureKind.Argument,
"Arg_MustContainEnumInfo",
null);
357 if (
char.IsDigit(value[0]) || value[0] ==
'-' || value[0] ==
'+')
363 parseResult.parsedEnum =
ToObject(enumType, value2);
366 catch (FormatException)
369 catch (Exception failure)
371 if (parseResult.canThrow)
375 parseResult.SetFailure(failure);
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++)
385 array[i] = array[i].Trim();
387 for (
int j = 0; j < names.Length; j++)
391 if (
string.Compare(names[j], array[i],
StringComparison.OrdinalIgnoreCase) != 0)
396 else if (!names[j].
Equals(array[i]))
400 ulong num2 = values[j];
407 parseResult.SetFailure(ParseFailureKind.ArgumentWithParameter,
"Arg_EnumValueNotFound", value);
413 parseResult.parsedEnum =
ToObject(enumType, num);
416 catch (Exception failure2)
418 if (parseResult.canThrow)
422 parseResult.SetFailure(failure2);
435 [__DynamicallyInvokable]
438 if (enumType ==
null)
456 [__DynamicallyInvokable]
459 if (enumType ==
null)
466 internal static ulong[] InternalGetValues(RuntimeType enumType)
468 return GetCachedValuesAndNames(enumType, getNames:
false).Values;
481 [__DynamicallyInvokable]
484 if (enumType ==
null)
499 [__DynamicallyInvokable]
502 if (enumType ==
null)
509 internal static string[] InternalGetNames(RuntimeType enumType)
511 return GetCachedValuesAndNames(enumType, getNames:
true).Names;
524 [__DynamicallyInvokable]
532 if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8 && (typeCode ==
TypeCode.Boolean || typeCode ==
TypeCode.Char))
539 return ToObject(enumType, (
int)value);
541 return ToObject(enumType, (sbyte)value);
543 return ToObject(enumType, (
short)value);
545 return ToObject(enumType, (
long)value);
547 return ToObject(enumType, (uint)value);
549 return ToObject(enumType, (
byte)value);
551 return ToObject(enumType, (ushort)value);
553 return ToObject(enumType, (ulong)value);
555 return ToObject(enumType, (
char)value);
557 return ToObject(enumType, (
bool)value);
576 [__DynamicallyInvokable]
579 if (enumType ==
null)
597 [__DynamicallyInvokable]
598 public static string Format(
Type enumType,
object value,
string format)
600 if (enumType ==
null)
616 RuntimeType runtimeType = enumType as RuntimeType;
617 if (runtimeType ==
null)
621 Type type = value.GetType();
630 type = underlyingType2;
631 value = ((
Enum)value).GetValue();
633 else if (type != underlyingType)
637 if (format.Length != 1)
645 return value.ToString();
648 return InternalFormattedHexString(value);
651 return InternalFormat(runtimeType, value);
654 return InternalFlagsFormat(runtimeType, value);
660 [SecuritySafeCritical]
661 internal unsafe
object GetValue()
663 fixed (
IntPtr* ptr = (
IntPtr*)(&JitHelpers.GetPinningHelper(
this).m_data))
665 switch (InternalGetCorElementType())
667 case CorElementType.I1:
669 case CorElementType.U1:
671 case CorElementType.Boolean:
672 return *(
byte*)ptr != 0;
673 case CorElementType.I2:
675 case CorElementType.U2:
676 return *(ushort*)ptr;
677 case CorElementType.Char:
678 return (
char)(*(ushort*)ptr);
679 case CorElementType.I4:
681 case CorElementType.U4:
683 case CorElementType.R4:
685 case CorElementType.I8:
687 case CorElementType.U8:
688 return (ulong)(*(
long*)ptr);
689 case CorElementType.R8:
690 return *(
double*)ptr;
691 case CorElementType.I:
693 case CorElementType.U:
694 return (
UIntPtr)(
void*)(
long)(*ptr);
703 private extern bool InternalHasFlag(
Enum flags);
706 [SecuritySafeCritical]
707 private extern CorElementType InternalGetCorElementType();
714 [SecuritySafeCritical]
715 [__DynamicallyInvokable]
716 public override extern bool Equals(
object obj);
720 [SecuritySafeCritical]
721 [__DynamicallyInvokable]
724 fixed (
IntPtr* ptr = (
IntPtr*)(&JitHelpers.GetPinningHelper(
this).m_data))
726 switch (InternalGetCorElementType())
728 case CorElementType.I1:
730 case CorElementType.U1:
732 case CorElementType.Boolean:
734 case CorElementType.I2:
736 case CorElementType.U2:
738 case CorElementType.Char:
740 case CorElementType.I4:
742 case CorElementType.U4:
744 case CorElementType.R4:
746 case CorElementType.I8:
748 case CorElementType.U8:
750 case CorElementType.R8:
752 case CorElementType.I:
753 return ptr->GetHashCode();
754 case CorElementType.U:
755 return ((
UIntPtr*)ptr)->GetHashCode();
764 [__DynamicallyInvokable]
767 return InternalFormat((RuntimeType)GetType(), GetValue());
778 [Obsolete(
"The provider argument is not used. Please use ToString(String).")]
791 [SecuritySafeCritical]
792 [__DynamicallyInvokable]
799 int num = InternalCompareTo(
this, target);
806 Type type = GetType();
820 [__DynamicallyInvokable]
823 if (format ==
null || format.Length == 0)
833 return GetValue().ToString();
837 return InternalFormattedHexString(GetValue());
841 return InternalFlagsFormat((RuntimeType)GetType(), GetValue());
849 [Obsolete(
"The provider argument is not used. Please use ToString().")]
861 [SecuritySafeCritical]
862 [__DynamicallyInvokable]
869 if (!GetType().IsEquivalentTo(flag.GetType()))
873 return InternalHasFlag(flag);
881 Type type = GetType();
883 if (underlyingType == typeof(
int))
887 if (underlyingType == typeof(sbyte))
891 if (underlyingType == typeof(
short))
895 if (underlyingType == typeof(
long))
899 if (underlyingType == typeof(uint))
903 if (underlyingType == typeof(
byte))
907 if (underlyingType == typeof(ushort))
911 if (underlyingType == typeof(ulong))
915 if (underlyingType == typeof(
bool))
919 if (underlyingType == typeof(
char))
930 [__DynamicallyInvokable]
940 [__DynamicallyInvokable]
941 char IConvertible.ToChar(IFormatProvider provider)
949 [__DynamicallyInvokable]
950 sbyte IConvertible.ToSByte(IFormatProvider provider)
958 [__DynamicallyInvokable]
959 byte IConvertible.ToByte(IFormatProvider provider)
967 [__DynamicallyInvokable]
968 short IConvertible.ToInt16(IFormatProvider provider)
976 [__DynamicallyInvokable]
977 ushort IConvertible.ToUInt16(IFormatProvider provider)
985 [__DynamicallyInvokable]
986 int IConvertible.ToInt32(IFormatProvider provider)
994 [__DynamicallyInvokable]
995 uint IConvertible.ToUInt32(IFormatProvider provider)
1003 [__DynamicallyInvokable]
1004 long IConvertible.ToInt64(IFormatProvider provider)
1012 [__DynamicallyInvokable]
1013 ulong IConvertible.ToUInt64(IFormatProvider provider)
1022 [__DynamicallyInvokable]
1023 float IConvertible.ToSingle(IFormatProvider provider)
1032 [__DynamicallyInvokable]
1033 double IConvertible.ToDouble(IFormatProvider provider)
1042 [__DynamicallyInvokable]
1043 decimal IConvertible.ToDecimal(IFormatProvider provider)
1052 [__DynamicallyInvokable]
1053 DateTime IConvertible.ToDateTime(IFormatProvider provider)
1055 throw new InvalidCastException(Environment.GetResourceString(
"InvalidCast_FromTo",
"Enum",
"DateTime"));
1062 [__DynamicallyInvokable]
1063 object IConvertible.ToType(Type type, IFormatProvider provider)
1065 return Convert.DefaultToType(
this, type, provider);
1076 [SecuritySafeCritical]
1077 [CLSCompliant(
false)]
1081 if (enumType ==
null)
1089 RuntimeType runtimeType = enumType as RuntimeType;
1090 if (runtimeType ==
null)
1094 return InternalBoxEnum(runtimeType, value);
1105 [SecuritySafeCritical]
1109 if (enumType ==
null)
1117 RuntimeType runtimeType = enumType as RuntimeType;
1118 if (runtimeType ==
null)
1122 return InternalBoxEnum(runtimeType, value);
1133 [SecuritySafeCritical]
1137 if (enumType ==
null)
1145 RuntimeType runtimeType = enumType as RuntimeType;
1146 if (runtimeType ==
null)
1150 return InternalBoxEnum(runtimeType, value);
1161 [SecuritySafeCritical]
1165 if (enumType ==
null)
1173 RuntimeType runtimeType = enumType as RuntimeType;
1174 if (runtimeType ==
null)
1178 return InternalBoxEnum(runtimeType, value);
1189 [SecuritySafeCritical]
1190 [CLSCompliant(
false)]
1194 if (enumType ==
null)
1202 RuntimeType runtimeType = enumType as RuntimeType;
1203 if (runtimeType ==
null)
1207 return InternalBoxEnum(runtimeType, value);
1218 [SecuritySafeCritical]
1219 [CLSCompliant(
false)]
1223 if (enumType ==
null)
1231 RuntimeType runtimeType = enumType as RuntimeType;
1232 if (runtimeType ==
null)
1236 return InternalBoxEnum(runtimeType, value);
1247 [SecuritySafeCritical]
1251 if (enumType ==
null)
1259 RuntimeType runtimeType = enumType as RuntimeType;
1260 if (runtimeType ==
null)
1264 return InternalBoxEnum(runtimeType, value);
1275 [SecuritySafeCritical]
1276 [CLSCompliant(
false)]
1280 if (enumType ==
null)
1288 RuntimeType runtimeType = enumType as RuntimeType;
1289 if (runtimeType ==
null)
1293 return InternalBoxEnum(runtimeType, (
long)value);
1296 [SecuritySafeCritical]
1297 private static object ToObject(
Type enumType,
char value)
1299 if (enumType ==
null)
1305 throw new ArgumentException(Environment.GetResourceString(
"Arg_MustBeEnum"),
"enumType");
1307 RuntimeType runtimeType = enumType as RuntimeType;
1308 if (runtimeType ==
null)
1310 throw new ArgumentException(Environment.GetResourceString(
"Arg_MustBeType"),
"enumType");
1312 return InternalBoxEnum(runtimeType, value);
1315 [SecuritySafeCritical]
1316 private static object ToObject(Type enumType,
bool value)
1318 if (enumType ==
null)
1320 throw new ArgumentNullException(
"enumType");
1322 if (!enumType.IsEnum)
1324 throw new ArgumentException(Environment.GetResourceString(
"Arg_MustBeEnum"),
"enumType");
1326 RuntimeType runtimeType = enumType as RuntimeType;
1327 if (runtimeType ==
null)
1329 throw new ArgumentException(Environment.GetResourceString(
"Arg_MustBeType"),
"enumType");
1331 return InternalBoxEnum(runtimeType, value ? 1 : 0);
1335 [__DynamicallyInvokable]
static object ToObject(Type enumType, int value)
Converts the specified 32-bit signed integer to an enumeration member.
Converts a base data type to another base data type.
A platform-specific type that is used to represent a pointer or a handle.
static string GetName(Type enumType, object value)
Retrieves the name of the constant in the specified enumeration that has the specified value.
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
static long ToInt64(object value)
Converts the value of the specified object to a 64-bit signed integer.
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.
string ToString(string format, IFormatProvider provider)
This method overload is obsolete; use M:System.Enum.ToString(System.String).
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.
virtual bool IsEquivalentTo(Type other)
Determines whether two COM types have the same identity and are eligible for type equivalence.
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.
No initialization action.
string ToString(string format)
Converts the value of this instance to its equivalent string representation using the specified forma...
static object ToObject(Type enumType, byte value)
Converts the specified 8-bit unsigned integer to an enumeration member.
override string ToString()
Returns a String representing the name of the current Type.
override string ToString()
Converts the value of this instance to its equivalent string representation.
static object ToObject(Type enumType, ushort value)
Converts the specified 16-bit unsigned integer value to an enumeration member.
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...
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...
Enum()
Initializes a new instance of the T:System.Enum class.
static string [] GetNames(Type enumType)
Retrieves an array of the names of the constants in a specified enumeration.
static object ToObject(Type enumType, long value)
Converts the specified 64-bit signed integer to an enumeration member.
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.
Defines a generalized type-specific comparison method that a value type or class implements to order ...
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...
static object ToObject(Type enumType, uint value)
Converts the specified 32-bit unsigned integer value to an enumeration member.
virtual string [] GetEnumNames()
Returns the names of the members of the current enumeration type.
Provides information about, and means to manipulate, the current environment and platform....
TypeCode GetTypeCode()
Returns the type code of the underlying type of this enumeration member.
static bool ToBoolean(object value)
Converts the value of a specified object to an equivalent Boolean value.
static bool IsDefined(Type enumType, object value)
Returns an indication whether a constant with a specified value exists in a specified enumeration.
A cast or conversion operation, such as (SampleType)obj in C::or CType(obj, SampleType) in Visual Bas...
Provides the base class for enumerations.
A platform-specific type that is used to represent a pointer or a handle.
static object ToObject(Type enumType, sbyte value)
Converts the specified 8-bit signed integer value to an enumeration member.
static Array GetValues(Type enumType)
Retrieves an array of the values of the constants in a specified enumeration.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Represents type declarations: class types, interface types, array types, value types,...
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.
MethodImplOptions
Defines the details of how a method is implemented.
CharSet
Dictates which character set marshaled strings should use.
static TypeCode GetTypeCode(object value)
Returns the T:System.TypeCode for the specified object.
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...
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.
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
virtual Type GetEnumUnderlyingType()
Returns the underlying type of the current enumeration type.
static byte ToByte(object value)
Converts the value of the specified object to an 8-bit unsigned integer.
virtual bool IsEnumDefined(object value)
Returns a value that indicates whether the specified value exists in the current enumeration type.
static object ToObject(Type enumType, short value)
Converts the specified 16-bit signed integer to an enumeration member.
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 ...
static Type GetUnderlyingType(Type enumType)
Returns the underlying type of the specified enumeration.
static ulong ToUInt64(object value)
Converts the value of the specified object to a 64-bit unsigned integer.
int CompareTo(object target)
Compares this instance to a specified object and returns an indication of their relative values.
Provides information about a specific culture (called a locale for unmanaged code development)....
static object ToObject(Type enumType, ulong value)
Converts the specified 64-bit unsigned integer value to an enumeration member.
Defines methods that convert the value of the implementing reference or value type to a common langua...
static string Format(Type enumType, object value, string format)
Converts the specified value of a specified enumerated type to its equivalent string representation a...
unsafe override int GetHashCode()
Returns the hash code for the value of this instance.
virtual Array GetEnumValues()
Returns an array of the values of the constants in the current enumeration type.