<Compile Include="$(BclSourcesRoot)\System\Diagnostics\SymbolStore\ISymWriter.cs" />
<Compile Include="$(BclSourcesRoot)\System\Diagnostics\SymbolStore\SymAddressKind.cs" />
<Compile Include="$(BclSourcesRoot)\System\Diagnostics\SymbolStore\Token.cs" />
- <Compile Include="$(BclSourcesRoot)\System\Enum.cs" />
+ <Compile Include="$(BclSourcesRoot)\System\Enum.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Environment.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Exception.cs" />
<Compile Include="$(BclSourcesRoot)\System\GC.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.SpecialFolder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.SpecialFolderOption.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\EnvironmentVariableTarget.cs" />
+ <Compile Include="$(MSBuildThisFileDirectory)System\Enum.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\EventArgs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\EventHandler.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\ExecutionEngineException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\BindingFlags.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\CallingConventions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\ConstructorInfo.cs" />
+ <Compile Include="$(MSBuildThisFileDirectory)System\Reflection\CorElementType.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\CustomAttributeFormatException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\DefaultMemberAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\AssemblyBuilderAccess.cs" />
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
-using System.Text;
using Internal.Runtime.CompilerServices;
+#if CORERT
+using CorElementType = System.Runtime.RuntimeImports.RhCorElementType;
+using RuntimeType = System.Type;
+#endif
+
// The code below includes partial support for float/double and
// pointer sized enums.
//
{
[Serializable]
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
- public abstract class Enum : ValueType, IComparable, IFormattable, IConvertible
+ public abstract partial class Enum : ValueType, IComparable, IFormattable, IConvertible
{
#region Private Constants
private const char EnumSeparatorChar = ',';
#endregion
#region Private Static Methods
- private static TypeValuesAndNames GetCachedValuesAndNames(RuntimeType enumType, bool getNames)
- {
- TypeValuesAndNames entry = enumType.GenericCache as TypeValuesAndNames;
-
- if (entry == null || (getNames && entry.Names == null))
- {
- ulong[] values = null;
- string[] names = null;
- GetEnumValuesAndNames(
- enumType.GetTypeHandleInternal(),
- JitHelpers.GetObjectHandleOnStack(ref values),
- JitHelpers.GetObjectHandleOnStack(ref names),
- getNames);
- bool isFlags = enumType.IsDefined(typeof(FlagsAttribute), inherit: false);
-
- entry = new TypeValuesAndNames(isFlags, values, names);
- enumType.GenericCache = entry;
- }
-
- return entry;
- }
private string ValueToString()
{
internal static string GetEnumName(RuntimeType eT, ulong ulValue)
{
Debug.Assert(eT != null);
- ulong[] ulValues = Enum.InternalGetValues(eT);
+ ulong[] ulValues = InternalGetValues(eT);
int index = Array.BinarySearch(ulValues, ulValue);
if (index >= 0)
{
- string[] names = Enum.InternalGetNames(eT);
+ string[] names = InternalGetNames(eT);
return names[index];
}
if (!entry.IsFlag) // Not marked with Flags attribute
{
- return Enum.GetEnumName(eT, value);
+ return GetEnumName(eT, value);
}
else // These are flags OR'ed together (We treat everything as unsigned types)
{
return result;
}
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private static extern int InternalCompareTo(object o1, object o2);
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- internal static extern RuntimeType InternalGetUnderlyingType(RuntimeType enumType);
-
- [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
- private static extern void GetEnumValuesAndNames(RuntimeTypeHandle enumType, ObjectHandleOnStack values, ObjectHandleOnStack names, bool getNames);
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private static extern object InternalBoxEnum(RuntimeType enumType, long value);
#endregion
#region Public Static Methods
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool StartsNumber(char c) => char.IsInRange(c, '0', '9') || c == '-' || c == '+';
- public static Type GetUnderlyingType(Type enumType)
- {
- if (enumType == null)
- throw new ArgumentNullException(nameof(enumType));
-
- return enumType.GetEnumUnderlyingType();
- }
-
- public static Array GetValues(Type enumType)
- {
- if (enumType == null)
- throw new ArgumentNullException(nameof(enumType));
-
- return enumType.GetEnumValues();
- }
-
internal static ulong[] InternalGetValues(RuntimeType enumType)
{
// Get all of the values
return GetCachedValuesAndNames(enumType, false).Values;
}
- public static string GetName(Type enumType, object value)
- {
- if (enumType == null)
- throw new ArgumentNullException(nameof(enumType));
-
- return enumType.GetEnumName(value);
- }
-
- public static string[] GetNames(Type enumType)
- {
- if (enumType == null)
- throw new ArgumentNullException(nameof(enumType));
-
- return enumType.GetEnumNames();
- }
-
internal static string[] InternalGetNames(RuntimeType enumType)
{
// Get all of the names
if (format == null)
throw new ArgumentNullException(nameof(format));
-
+
// If the value is an Enum then we need to extract the underlying value from it
Type valueType = value.GetType();
if (valueType.IsEnum)
}
return ((Enum)value).ToString(format);
}
-
+
// The value must be of the same type as the Underlying type of the Enum
Type underlyingType = GetUnderlyingType(enumType);
if (valueType != underlyingType)
}
}
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern bool InternalHasFlag(Enum flags);
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern CorElementType InternalGetCorElementType();
-
#endregion
#region Object Overrides
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- public extern override bool Equals(object obj);
public override int GetHashCode()
{
return ToString();
}
- [Intrinsic]
- public bool HasFlag(Enum flag)
- {
- if (flag == null)
- throw new ArgumentNullException(nameof(flag));
-
- if (!this.GetType().IsEquivalentTo(flag.GetType()))
- {
- throw new ArgumentException(SR.Format(SR.Argument_EnumTypeDoesNotMatch, flag.GetType(), this.GetType()));
- }
-
- return InternalHasFlag(flag);
- }
-
#endregion
- #region IConvertable
+ #region IConvertible
public TypeCode GetTypeCode()
{
switch (InternalGetCorElementType())
private static object ToObject(Type enumType, bool value) =>
InternalBoxEnum(ValidateRuntimeType(enumType), value ? 1 : 0);
- private static RuntimeType ValidateRuntimeType(Type enumType)
- {
- if (enumType == null)
- throw new ArgumentNullException(nameof(enumType));
- if (!enumType.IsEnum)
- throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType));
- if (!(enumType is RuntimeType rtType))
- throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType));
- return rtType;
- }
#endregion
}
}
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+namespace System.Reflection
+{
+ internal enum CorElementType : byte
+ {
+ ELEMENT_TYPE_END = 0x00,
+ ELEMENT_TYPE_VOID = 0x01,
+ ELEMENT_TYPE_BOOLEAN = 0x02,
+ ELEMENT_TYPE_CHAR = 0x03,
+ ELEMENT_TYPE_I1 = 0x04,
+ ELEMENT_TYPE_U1 = 0x05,
+ ELEMENT_TYPE_I2 = 0x06,
+ ELEMENT_TYPE_U2 = 0x07,
+ ELEMENT_TYPE_I4 = 0x08,
+ ELEMENT_TYPE_U4 = 0x09,
+ ELEMENT_TYPE_I8 = 0x0A,
+ ELEMENT_TYPE_U8 = 0x0B,
+ ELEMENT_TYPE_R4 = 0x0C,
+ ELEMENT_TYPE_R8 = 0x0D,
+ ELEMENT_TYPE_STRING = 0x0E,
+ ELEMENT_TYPE_PTR = 0x0F,
+ ELEMENT_TYPE_BYREF = 0x10,
+ ELEMENT_TYPE_VALUETYPE = 0x11,
+ ELEMENT_TYPE_CLASS = 0x12,
+ ELEMENT_TYPE_VAR = 0x13,
+ ELEMENT_TYPE_ARRAY = 0x14,
+ ELEMENT_TYPE_GENERICINST = 0x15,
+ ELEMENT_TYPE_TYPEDBYREF = 0x16,
+ ELEMENT_TYPE_I = 0x18,
+ ELEMENT_TYPE_U = 0x19,
+ ELEMENT_TYPE_FNPTR = 0x1B,
+ ELEMENT_TYPE_OBJECT = 0x1C,
+ ELEMENT_TYPE_SZARRAY = 0x1D,
+ ELEMENT_TYPE_MVAR = 0x1E,
+ ELEMENT_TYPE_CMOD_REQD = 0x1F,
+ ELEMENT_TYPE_CMOD_OPT = 0x20,
+ ELEMENT_TYPE_INTERNAL = 0x21,
+ ELEMENT_TYPE_MAX = 0x22,
+ ELEMENT_TYPE_MODIFIER = 0x40,
+ ELEMENT_TYPE_SENTINEL = 0x41,
+ ELEMENT_TYPE_PINNED = 0x45,
+ }
+}
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+namespace System
+{
+ public abstract partial class Enum
+ {
+ [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
+ private static extern void GetEnumValuesAndNames(RuntimeTypeHandle enumType, ObjectHandleOnStack values, ObjectHandleOnStack names, bool getNames);
+
+ [MethodImpl(MethodImplOptions.InternalCall)]
+ public override extern bool Equals(object obj);
+
+ [MethodImpl(MethodImplOptions.InternalCall)]
+ private static extern object InternalBoxEnum(RuntimeType enumType, long value);
+
+ [MethodImpl(MethodImplOptions.InternalCall)]
+ private static extern int InternalCompareTo(object o1, object o2);
+
+ [MethodImpl(MethodImplOptions.InternalCall)]
+ private extern CorElementType InternalGetCorElementType();
+
+ [MethodImpl(MethodImplOptions.InternalCall)]
+ internal static extern RuntimeType InternalGetUnderlyingType(RuntimeType enumType);
+
+ [MethodImpl(MethodImplOptions.InternalCall)]
+ private extern bool InternalHasFlag(Enum flags);
+
+ private static TypeValuesAndNames GetCachedValuesAndNames(RuntimeType enumType, bool getNames)
+ {
+ TypeValuesAndNames entry = enumType.GenericCache as TypeValuesAndNames;
+
+ if (entry == null || (getNames && entry.Names == null))
+ {
+ ulong[] values = null;
+ string[] names = null;
+ GetEnumValuesAndNames(
+ enumType.GetTypeHandleInternal(),
+ JitHelpers.GetObjectHandleOnStack(ref values),
+ JitHelpers.GetObjectHandleOnStack(ref names),
+ getNames);
+ bool isFlags = enumType.IsDefined(typeof(FlagsAttribute), inherit: false);
+
+ entry = new TypeValuesAndNames(isFlags, values, names);
+ enumType.GenericCache = entry;
+ }
+
+ return entry;
+ }
+
+ [Intrinsic]
+ public bool HasFlag(Enum flag)
+ {
+ if (flag == null)
+ throw new ArgumentNullException(nameof(flag));
+
+ if (!this.GetType().IsEquivalentTo(flag.GetType()))
+ {
+ throw new ArgumentException(SR.Format(SR.Argument_EnumTypeDoesNotMatch, flag.GetType(), this.GetType()));
+ }
+
+ return InternalHasFlag(flag);
+ }
+
+ public static string GetName(Type enumType, object value)
+ {
+ if (enumType == null)
+ throw new ArgumentNullException(nameof(enumType));
+
+ return enumType.GetEnumName(value);
+ }
+
+ public static string[] GetNames(Type enumType)
+ {
+ if (enumType == null)
+ throw new ArgumentNullException(nameof(enumType));
+
+ return enumType.GetEnumNames();
+ }
+
+ public static Type GetUnderlyingType(Type enumType)
+ {
+ if (enumType == null)
+ throw new ArgumentNullException(nameof(enumType));
+
+ return enumType.GetEnumUnderlyingType();
+ }
+
+ public static Array GetValues(Type enumType)
+ {
+ if (enumType == null)
+ throw new ArgumentNullException(nameof(enumType));
+
+ return enumType.GetEnumValues();
+ }
+
+ private static RuntimeType ValidateRuntimeType(Type enumType)
+ {
+ if (enumType == null)
+ throw new ArgumentNullException(nameof(enumType));
+ if (!enumType.IsEnum)
+ throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType));
+ if (!(enumType is RuntimeType rtType))
+ throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType));
+ return rtType;
+ }
+ }
+}
namespace System.Reflection
{
- internal enum CorElementType : byte
- {
- ELEMENT_TYPE_END = 0x00,
- ELEMENT_TYPE_VOID = 0x01,
- ELEMENT_TYPE_BOOLEAN = 0x02,
- ELEMENT_TYPE_CHAR = 0x03,
- ELEMENT_TYPE_I1 = 0x04,
- ELEMENT_TYPE_U1 = 0x05,
- ELEMENT_TYPE_I2 = 0x06,
- ELEMENT_TYPE_U2 = 0x07,
- ELEMENT_TYPE_I4 = 0x08,
- ELEMENT_TYPE_U4 = 0x09,
- ELEMENT_TYPE_I8 = 0x0A,
- ELEMENT_TYPE_U8 = 0x0B,
- ELEMENT_TYPE_R4 = 0x0C,
- ELEMENT_TYPE_R8 = 0x0D,
- ELEMENT_TYPE_STRING = 0x0E,
- ELEMENT_TYPE_PTR = 0x0F,
- ELEMENT_TYPE_BYREF = 0x10,
- ELEMENT_TYPE_VALUETYPE = 0x11,
- ELEMENT_TYPE_CLASS = 0x12,
- ELEMENT_TYPE_VAR = 0x13,
- ELEMENT_TYPE_ARRAY = 0x14,
- ELEMENT_TYPE_GENERICINST = 0x15,
- ELEMENT_TYPE_TYPEDBYREF = 0x16,
- ELEMENT_TYPE_I = 0x18,
- ELEMENT_TYPE_U = 0x19,
- ELEMENT_TYPE_FNPTR = 0x1B,
- ELEMENT_TYPE_OBJECT = 0x1C,
- ELEMENT_TYPE_SZARRAY = 0x1D,
- ELEMENT_TYPE_MVAR = 0x1E,
- ELEMENT_TYPE_CMOD_REQD = 0x1F,
- ELEMENT_TYPE_CMOD_OPT = 0x20,
- ELEMENT_TYPE_INTERNAL = 0x21,
- ELEMENT_TYPE_MAX = 0x22,
- ELEMENT_TYPE_MODIFIER = 0x40,
- ELEMENT_TYPE_SENTINEL = 0x41,
- ELEMENT_TYPE_PINNED = 0x45,
- }
-
[Flags()]
internal enum MdSigCallingConvention : byte
{