// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+// Rare enums types not expressible in C# are not supported in native AOT
+#if !NATIVEAOT
+#define RARE_ENUMS
+#endif
+
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
RuntimeType rt = (RuntimeType)typeof(TEnum);
Type underlyingType = typeof(TEnum).GetEnumUnderlyingType();
+ if (underlyingType == typeof(sbyte)) return GetNameInlined(GetEnumInfo<sbyte>(rt), *(sbyte*)&value);
+ if (underlyingType == typeof(byte)) return GetNameInlined(GetEnumInfo<byte>(rt), *(byte*)&value);
+ if (underlyingType == typeof(short)) return GetNameInlined(GetEnumInfo<short>(rt), *(short*)&value);
+ if (underlyingType == typeof(ushort)) return GetNameInlined(GetEnumInfo<ushort>(rt), *(ushort*)&value);
if (underlyingType == typeof(int)) return GetNameInlined(GetEnumInfo<int>(rt), *(int*)&value);
if (underlyingType == typeof(uint)) return GetNameInlined(GetEnumInfo<uint>(rt), *(uint*)&value);
if (underlyingType == typeof(long)) return GetNameInlined(GetEnumInfo<long>(rt), *(long*)&value);
if (underlyingType == typeof(ulong)) return GetNameInlined(GetEnumInfo<ulong>(rt), *(ulong*)&value);
- if (underlyingType == typeof(byte)) return GetNameInlined(GetEnumInfo<byte>(rt), *(byte*)&value);
- if (underlyingType == typeof(sbyte)) return GetNameInlined(GetEnumInfo<sbyte>(rt), *(sbyte*)&value);
- if (underlyingType == typeof(short)) return GetNameInlined(GetEnumInfo<short>(rt), *(short*)&value);
- if (underlyingType == typeof(ushort)) return GetNameInlined(GetEnumInfo<ushort>(rt), *(ushort*)&value);
+#if RARE_ENUMS
if (underlyingType == typeof(nint)) return GetNameInlined(GetEnumInfo<nint>(rt), *(nint*)&value);
if (underlyingType == typeof(nuint)) return GetNameInlined(GetEnumInfo<nuint>(rt), *(nuint*)&value);
- if (underlyingType == typeof(char)) return GetNameInlined(GetEnumInfo<char>(rt), *(char*)&value);
if (underlyingType == typeof(float)) return GetNameInlined(GetEnumInfo<float>(rt), *(float*)&value);
if (underlyingType == typeof(double)) return GetNameInlined(GetEnumInfo<double>(rt), *(double*)&value);
+ if (underlyingType == typeof(char)) return GetNameInlined(GetEnumInfo<char>(rt), *(char*)&value);
if (underlyingType == typeof(bool)) return GetNameInlined(GetEnumInfo<byte>(rt), *(bool*)&value ? (byte)1 : (byte)0);
-
+#endif
throw CreateUnknownEnumTypeException();
}
if (uint64Value > byte.MaxValue) return null;
return GetName(GetEnumInfo<byte>(enumType), (byte)uint64Value);
- case TypeCode.Boolean:
- if (uint64Value > 1) return null;
- return GetName(GetEnumInfo<byte>(enumType), (byte)uint64Value);
-
case TypeCode.Int16:
if ((long)uint64Value < short.MinValue || (long)uint64Value > short.MaxValue) return null;
return GetName(GetEnumInfo<short>(enumType), (short)uint64Value);
if (uint64Value > ushort.MaxValue) return null;
return GetName(GetEnumInfo<ushort>(enumType), (ushort)uint64Value);
- case TypeCode.Char:
- if (uint64Value > char.MaxValue) return null;
- return GetName(GetEnumInfo<char>(enumType), (char)uint64Value);
+ case TypeCode.Int32:
+ if ((long)uint64Value < int.MinValue || (long)uint64Value > int.MaxValue) return null;
+ return GetName(GetEnumInfo<int>(enumType), (int)uint64Value);
case TypeCode.UInt32:
if (uint64Value > uint.MaxValue) return null;
return GetName(GetEnumInfo<uint>(enumType), (uint)uint64Value);
- case TypeCode.Int32:
- if ((long)uint64Value < int.MinValue || (long)uint64Value > int.MaxValue) return null;
- return GetName(GetEnumInfo<int>(enumType), (int)uint64Value);
+ case TypeCode.Int64:
+ return GetName(GetEnumInfo<long>(enumType), (long)uint64Value);
case TypeCode.UInt64:
return GetName(GetEnumInfo<ulong>(enumType), uint64Value);
- case TypeCode.Int64:
- return GetName(GetEnumInfo<long>(enumType), (long)uint64Value);
+#if RARE_ENUMS
+ case TypeCode.Char:
+ if (uint64Value > char.MaxValue) return null;
+ return GetName(GetEnumInfo<char>(enumType), (char)uint64Value);
+
+ case TypeCode.Boolean:
+ if (uint64Value > 1) return null;
+ return GetName(GetEnumInfo<byte>(enumType), (byte)uint64Value);
+#endif
};
+#if RARE_ENUMS
if (underlyingType == typeof(nint))
{
if ((long)uint64Value < nint.MinValue || (long)uint64Value > nint.MaxValue) return null;
if (uint64Value > nuint.MaxValue) return null;
return GetName(GetEnumInfo<nuint>(enumType), (nuint)uint64Value);
}
+#endif
throw CreateUnknownEnumTypeException();
}
Type underlyingType = typeof(TEnum).GetEnumUnderlyingType();
// Get the cached names array.
- if (underlyingType == typeof(int)) names = GetEnumInfo<int>(rt).Names;
- else if (underlyingType == typeof(uint)) names = GetEnumInfo<uint>(rt).Names;
- else if (underlyingType == typeof(long)) names = GetEnumInfo<long>(rt).Names;
- else if (underlyingType == typeof(ulong)) names = GetEnumInfo<ulong>(rt).Names;
+ if (underlyingType == typeof(sbyte)) names = GetEnumInfo<sbyte>(rt).Names;
else if (underlyingType == typeof(byte)) names = GetEnumInfo<byte>(rt).Names;
- else if (underlyingType == typeof(sbyte)) names = GetEnumInfo<sbyte>(rt).Names;
else if (underlyingType == typeof(short)) names = GetEnumInfo<short>(rt).Names;
else if (underlyingType == typeof(ushort)) names = GetEnumInfo<ushort>(rt).Names;
+ else if (underlyingType == typeof(int)) names = GetEnumInfo<int>(rt).Names;
+ else if (underlyingType == typeof(uint)) names = GetEnumInfo<uint>(rt).Names;
+ else if (underlyingType == typeof(long)) names = GetEnumInfo<long>(rt).Names;
+ else if (underlyingType == typeof(ulong)) names = GetEnumInfo<ulong>(rt).Names;
+#if RARE_ENUMS
else if (underlyingType == typeof(nint)) names = GetEnumInfo<nint>(rt).Names;
else if (underlyingType == typeof(nuint)) names = GetEnumInfo<nuint>(rt).Names;
- else if (underlyingType == typeof(char)) names = GetEnumInfo<char>(rt).Names;
else if (underlyingType == typeof(float)) names = GetEnumInfo<float>(rt).Names;
else if (underlyingType == typeof(double)) names = GetEnumInfo<double>(rt).Names;
+ else if (underlyingType == typeof(char)) names = GetEnumInfo<char>(rt).Names;
else if (underlyingType == typeof(bool)) names = GetEnumInfo<byte>(rt).Names;
+#endif
else throw CreateUnknownEnumTypeException();
// Return a clone of the array to avoid exposing the cached array instance.
CorElementType.ELEMENT_TYPE_U4 => GetEnumInfo<uint>(enumType).Names,
CorElementType.ELEMENT_TYPE_I8 => GetEnumInfo<long>(enumType).Names,
CorElementType.ELEMENT_TYPE_U8 => GetEnumInfo<ulong>(enumType).Names,
+#if RARE_ENUMS
CorElementType.ELEMENT_TYPE_R4 => GetEnumInfo<float>(enumType).Names,
CorElementType.ELEMENT_TYPE_R8 => GetEnumInfo<double>(enumType).Names,
CorElementType.ELEMENT_TYPE_I => GetEnumInfo<nint>(enumType).Names,
CorElementType.ELEMENT_TYPE_U => GetEnumInfo<nuint>(enumType).Names,
CorElementType.ELEMENT_TYPE_CHAR => GetEnumInfo<char>(enumType).Names,
CorElementType.ELEMENT_TYPE_BOOLEAN => GetEnumInfo<byte>(enumType).Names,
+#endif
_ => throw CreateUnknownEnumTypeException(),
};
}
CorElementType.ELEMENT_TYPE_U4 => GetEnumInfo<uint>(enumType, getNames: false).CloneValues(),
CorElementType.ELEMENT_TYPE_I8 => GetEnumInfo<long>(enumType, getNames: false).CloneValues(),
CorElementType.ELEMENT_TYPE_U8 => GetEnumInfo<ulong>(enumType, getNames: false).CloneValues(),
+#if RARE_ENUMS
CorElementType.ELEMENT_TYPE_R4 => GetEnumInfo<float>(enumType, getNames: false).CloneValues(),
CorElementType.ELEMENT_TYPE_R8 => GetEnumInfo<double>(enumType, getNames: false).CloneValues(),
CorElementType.ELEMENT_TYPE_I => GetEnumInfo<nint>(enumType, getNames: false).CloneValues(),
CorElementType.ELEMENT_TYPE_U => GetEnumInfo<nuint>(enumType, getNames: false).CloneValues(),
CorElementType.ELEMENT_TYPE_CHAR => GetEnumInfo<char>(enumType, getNames: false).CloneValues(),
CorElementType.ELEMENT_TYPE_BOOLEAN => CopyByteArrayToNewBoolArray(GetEnumInfo<byte>(enumType, getNames: false).Values),
+#endif
_ => throw CreateUnknownEnumTypeException(),
};
}
CorElementType.ELEMENT_TYPE_U4 => GetEnumInfo<uint>(enumType, getNames: false).Values,
CorElementType.ELEMENT_TYPE_I8 => GetEnumInfo<long>(enumType, getNames: false).Values,
CorElementType.ELEMENT_TYPE_U8 => GetEnumInfo<ulong>(enumType, getNames: false).Values,
+#if RARE_ENUMS
CorElementType.ELEMENT_TYPE_R4 => GetEnumInfo<float>(enumType, getNames: false).Values,
CorElementType.ELEMENT_TYPE_R8 => GetEnumInfo<double>(enumType, getNames: false).Values,
CorElementType.ELEMENT_TYPE_I => GetEnumInfo<nint>(enumType, getNames: false).Values,
CorElementType.ELEMENT_TYPE_U => GetEnumInfo<nuint>(enumType, getNames: false).Values,
CorElementType.ELEMENT_TYPE_CHAR => GetEnumInfo<char>(enumType, getNames: false).Values,
CorElementType.ELEMENT_TYPE_BOOLEAN => CopyByteArrayToNewBoolArray(GetEnumInfo<byte>(enumType, getNames: false).Values), // this is the only case that clones, out of necessity
+#endif
_ => throw CreateUnknownEnumTypeException(),
};
}
{
case CorElementType.ELEMENT_TYPE_I1:
case CorElementType.ELEMENT_TYPE_U1:
- case CorElementType.ELEMENT_TYPE_BOOLEAN:
{
byte flagsValue = pFlagsValue;
return (pThisValue & flagsValue) == flagsValue;
case CorElementType.ELEMENT_TYPE_I2:
case CorElementType.ELEMENT_TYPE_U2:
- case CorElementType.ELEMENT_TYPE_CHAR:
{
ushort flagsValue = Unsafe.As<byte, ushort>(ref pFlagsValue);
return (Unsafe.As<byte, ushort>(ref pThisValue) & flagsValue) == flagsValue;
case CorElementType.ELEMENT_TYPE_I4:
case CorElementType.ELEMENT_TYPE_U4:
-#if TARGET_32BIT
- case CorElementType.ELEMENT_TYPE_I:
- case CorElementType.ELEMENT_TYPE_U:
-#endif
- case CorElementType.ELEMENT_TYPE_R4:
{
uint flagsValue = Unsafe.As<byte, uint>(ref pFlagsValue);
return (Unsafe.As<byte, uint>(ref pThisValue) & flagsValue) == flagsValue;
case CorElementType.ELEMENT_TYPE_I8:
case CorElementType.ELEMENT_TYPE_U8:
-#if TARGET_64BIT
- case CorElementType.ELEMENT_TYPE_I:
- case CorElementType.ELEMENT_TYPE_U:
-#endif
- case CorElementType.ELEMENT_TYPE_R8:
{
ulong flagsValue = Unsafe.As<byte, ulong>(ref pFlagsValue);
return (Unsafe.As<byte, ulong>(ref pThisValue) & flagsValue) == flagsValue;
}
+#if RARE_ENUMS
+ case CorElementType.ELEMENT_TYPE_BOOLEAN:
+ goto case CorElementType.ELEMENT_TYPE_U1;
+
+ case CorElementType.ELEMENT_TYPE_CHAR:
+ goto case CorElementType.ELEMENT_TYPE_U2;
+
+ case CorElementType.ELEMENT_TYPE_R4:
+ goto case CorElementType.ELEMENT_TYPE_U4;
+
+ case CorElementType.ELEMENT_TYPE_R8:
+ goto case CorElementType.ELEMENT_TYPE_U8;
+
+ case CorElementType.ELEMENT_TYPE_I:
+ case CorElementType.ELEMENT_TYPE_U:
+#if TARGET_32BIT
+ goto case CorElementType.ELEMENT_TYPE_U4;
+#else
+ goto case CorElementType.ELEMENT_TYPE_U8;
+#endif
+#endif
+
default:
Debug.Fail("Unknown enum underlying type");
return false;
RuntimeType rt = (RuntimeType)typeof(TEnum);
Type underlyingType = typeof(TEnum).GetEnumUnderlyingType();
+ if (underlyingType == typeof(sbyte)) return IsDefinedPrimitive(rt, *(sbyte*)&value);
+ if (underlyingType == typeof(byte)) return IsDefinedPrimitive(rt, *(byte*)&value);
+ if (underlyingType == typeof(short)) return IsDefinedPrimitive(rt, *(short*)&value);
+ if (underlyingType == typeof(ushort)) return IsDefinedPrimitive(rt, *(ushort*)&value);
if (underlyingType == typeof(int)) return IsDefinedPrimitive(rt, *(int*)&value);
if (underlyingType == typeof(uint)) return IsDefinedPrimitive(rt, *(uint*)&value);
if (underlyingType == typeof(long)) return IsDefinedPrimitive(rt, *(long*)&value);
if (underlyingType == typeof(ulong)) return IsDefinedPrimitive(rt, *(ulong*)&value);
- if (underlyingType == typeof(byte)) return IsDefinedPrimitive(rt, *(byte*)&value);
- if (underlyingType == typeof(sbyte)) return IsDefinedPrimitive(rt, *(sbyte*)&value);
- if (underlyingType == typeof(short)) return IsDefinedPrimitive(rt, *(short*)&value);
- if (underlyingType == typeof(ushort)) return IsDefinedPrimitive(rt, *(ushort*)&value);
+#if RARE_ENUMS
if (underlyingType == typeof(nint)) return IsDefinedPrimitive(rt, *(nint*)&value);
if (underlyingType == typeof(nuint)) return IsDefinedPrimitive(rt, *(nuint*)&value);
- if (underlyingType == typeof(char)) return IsDefinedPrimitive(rt, *(char*)&value);
if (underlyingType == typeof(float)) return IsDefinedPrimitive(rt, *(float*)&value);
if (underlyingType == typeof(double)) return IsDefinedPrimitive(rt, *(double*)&value);
+ if (underlyingType == typeof(char)) return IsDefinedPrimitive(rt, *(char*)&value);
if (underlyingType == typeof(bool)) return IsDefinedPrimitive(rt, *(bool*)&value ? (byte)1 : (byte)0);
+#endif
throw CreateUnknownEnumTypeException();
}
[MethodImpl(MethodImplOptions.NoInlining)]
static bool TryParseRareTypes(RuntimeType rt, ReadOnlySpan<char> value, bool ignoreCase, bool throwOnFailure, [NotNullWhen(true)] out long result)
{
+#if RARE_ENUMS
bool parsed;
switch (InternalGetCorElementType(rt))
}
return parsed;
+#else
+ throw CreateUnknownEnumTypeException();
+#endif
}
}
RuntimeType rt = (RuntimeType)typeof(TEnum);
Type underlyingType = typeof(TEnum).GetEnumUnderlyingType();
+ if (underlyingType == typeof(sbyte)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, sbyte>(ref result));
+ if (underlyingType == typeof(byte)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, byte>(ref result));
+ if (underlyingType == typeof(short)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, short>(ref result));
+ if (underlyingType == typeof(ushort)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, ushort>(ref result));
if (underlyingType == typeof(int)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, int>(ref result));
if (underlyingType == typeof(uint)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, uint>(ref result));
if (underlyingType == typeof(long)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, long>(ref result));
if (underlyingType == typeof(ulong)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, ulong>(ref result));
- if (underlyingType == typeof(byte)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, byte>(ref result));
- if (underlyingType == typeof(sbyte)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, sbyte>(ref result));
- if (underlyingType == typeof(short)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, short>(ref result));
- if (underlyingType == typeof(ushort)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, ushort>(ref result));
+#if RARE_ENUMS
if (underlyingType == typeof(float)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, float>(ref result));
if (underlyingType == typeof(double)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, double>(ref result));
- if (underlyingType == typeof(char)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, char>(ref result));
if (underlyingType == typeof(nint)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, nint>(ref result));
if (underlyingType == typeof(nuint)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, nuint>(ref result));
+ if (underlyingType == typeof(char)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, char>(ref result));
if (underlyingType == typeof(bool)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, byte>(ref result));
+#endif
- if (throwOnFailure)
- {
- throw CreateUnknownEnumTypeException();
- }
- result = default;
- return false;
+ throw CreateUnknownEnumTypeException();
}
/// <summary>Core implementation for all {Try}Parse methods, both generic and non-generic, parsing either by value or by name.</summary>
}
else
{
- // Rare types not expressible in C#
+#if RARE_ENUMS
Type underlyingType = GetUnderlyingType(enumType);
try
{
{
status = Number.ParsingStatus.Overflow; // fall through to returning failure
}
+#else
+ throw CreateUnknownEnumTypeException();
+#endif
}
if (status != Number.ParsingStatus.Overflow)
{
case TypeCode.SByte: return (ulong)(sbyte)value;
case TypeCode.Byte: return (byte)value;
- case TypeCode.Boolean: return (bool)value ? 1UL : 0UL;
case TypeCode.Int16: return (ulong)(short)value;
case TypeCode.UInt16: return (ushort)value;
- case TypeCode.Char: return (char)value;
- case TypeCode.UInt32: return (uint)value;
case TypeCode.Int32: return (ulong)(int)value;
- case TypeCode.UInt64: return (ulong)value;
case TypeCode.Int64: return (ulong)(long)value;
+ case TypeCode.UInt32: return (uint)value;
+ case TypeCode.UInt64: return (ulong)value;
+ case TypeCode.Char: return (char)value;
+ case TypeCode.Boolean: return (bool)value ? 1UL : 0UL;
};
if (value is not null)
CorElementType.ELEMENT_TYPE_U4 => Unsafe.As<byte, uint>(ref data),
CorElementType.ELEMENT_TYPE_I8 => Unsafe.As<byte, long>(ref data),
CorElementType.ELEMENT_TYPE_U8 => Unsafe.As<byte, ulong>(ref data),
+#if RARE_ENUMS
CorElementType.ELEMENT_TYPE_R4 => Unsafe.As<byte, float>(ref data),
CorElementType.ELEMENT_TYPE_R8 => Unsafe.As<byte, double>(ref data),
CorElementType.ELEMENT_TYPE_I => Unsafe.As<byte, IntPtr>(ref data),
CorElementType.ELEMENT_TYPE_U => Unsafe.As<byte, UIntPtr>(ref data),
CorElementType.ELEMENT_TYPE_CHAR => Unsafe.As<byte, char>(ref data),
CorElementType.ELEMENT_TYPE_BOOLEAN => Unsafe.As<byte, bool>(ref data),
+#endif
_ => throw CreateUnknownEnumTypeException(),
};
}
{
case CorElementType.ELEMENT_TYPE_I1:
case CorElementType.ELEMENT_TYPE_U1:
- case CorElementType.ELEMENT_TYPE_BOOLEAN:
return pThisValue == pOtherValue;
case CorElementType.ELEMENT_TYPE_I2:
case CorElementType.ELEMENT_TYPE_U2:
- case CorElementType.ELEMENT_TYPE_CHAR:
return Unsafe.As<byte, ushort>(ref pThisValue) == Unsafe.As<byte, ushort>(ref pOtherValue);
case CorElementType.ELEMENT_TYPE_I4:
case CorElementType.ELEMENT_TYPE_U4:
-#if TARGET_32BIT
- case CorElementType.ELEMENT_TYPE_I:
- case CorElementType.ELEMENT_TYPE_U:
-#endif
- case CorElementType.ELEMENT_TYPE_R4:
return Unsafe.As<byte, uint>(ref pThisValue) == Unsafe.As<byte, uint>(ref pOtherValue);
case CorElementType.ELEMENT_TYPE_I8:
case CorElementType.ELEMENT_TYPE_U8:
-#if TARGET_64BIT
+ return Unsafe.As<byte, ulong>(ref pThisValue) == Unsafe.As<byte, ulong>(ref pOtherValue);
+
+#if RARE_ENUMS
+ case CorElementType.ELEMENT_TYPE_BOOLEAN:
+ goto case CorElementType.ELEMENT_TYPE_U1;
+
+ case CorElementType.ELEMENT_TYPE_CHAR:
+ goto case CorElementType.ELEMENT_TYPE_U2;
+
+ case CorElementType.ELEMENT_TYPE_R4:
+ goto case CorElementType.ELEMENT_TYPE_U4;
+
+ case CorElementType.ELEMENT_TYPE_R8:
+ goto case CorElementType.ELEMENT_TYPE_U8;
+
case CorElementType.ELEMENT_TYPE_I:
case CorElementType.ELEMENT_TYPE_U:
+#if TARGET_32BIT
+ goto case CorElementType.ELEMENT_TYPE_U4;
+#else
+ goto case CorElementType.ELEMENT_TYPE_U8;
+#endif
#endif
- case CorElementType.ELEMENT_TYPE_R8:
- return Unsafe.As<byte, ulong>(ref pThisValue) == Unsafe.As<byte, ulong>(ref pOtherValue);
default:
Debug.Fail("Unknown enum underlying type");
CorElementType.ELEMENT_TYPE_U4 => Unsafe.As<byte, uint>(ref data).GetHashCode(),
CorElementType.ELEMENT_TYPE_I8 => Unsafe.As<byte, long>(ref data).GetHashCode(),
CorElementType.ELEMENT_TYPE_U8 => Unsafe.As<byte, ulong>(ref data).GetHashCode(),
+#if RARE_ENUMS
CorElementType.ELEMENT_TYPE_R4 => Unsafe.As<byte, float>(ref data).GetHashCode(),
CorElementType.ELEMENT_TYPE_R8 => Unsafe.As<byte, double>(ref data).GetHashCode(),
CorElementType.ELEMENT_TYPE_I => Unsafe.As<byte, IntPtr>(ref data).GetHashCode(),
CorElementType.ELEMENT_TYPE_U => Unsafe.As<byte, UIntPtr>(ref data).GetHashCode(),
CorElementType.ELEMENT_TYPE_CHAR => Unsafe.As<byte, char>(ref data).GetHashCode(),
CorElementType.ELEMENT_TYPE_BOOLEAN => Unsafe.As<byte, bool>(ref data).GetHashCode(),
+#endif
_ => throw CreateUnknownEnumTypeException(),
};
}
return Unsafe.As<byte, sbyte>(ref pThisValue).CompareTo(Unsafe.As<byte, sbyte>(ref pTargetValue));
case CorElementType.ELEMENT_TYPE_U1:
- case CorElementType.ELEMENT_TYPE_BOOLEAN:
return pThisValue.CompareTo(pTargetValue);
case CorElementType.ELEMENT_TYPE_I2:
return Unsafe.As<byte, short>(ref pThisValue).CompareTo(Unsafe.As<byte, short>(ref pTargetValue));
case CorElementType.ELEMENT_TYPE_U2:
- case CorElementType.ELEMENT_TYPE_CHAR:
return Unsafe.As<byte, ushort>(ref pThisValue).CompareTo(Unsafe.As<byte, ushort>(ref pTargetValue));
case CorElementType.ELEMENT_TYPE_I4:
-#if TARGET_32BIT
- case CorElementType.ELEMENT_TYPE_I:
-#endif
return Unsafe.As<byte, int>(ref pThisValue).CompareTo(Unsafe.As<byte, int>(ref pTargetValue));
case CorElementType.ELEMENT_TYPE_U4:
-#if TARGET_32BIT
- case CorElementType.ELEMENT_TYPE_U:
-#endif
return Unsafe.As<byte, uint>(ref pThisValue).CompareTo(Unsafe.As<byte, uint>(ref pTargetValue));
case CorElementType.ELEMENT_TYPE_I8:
-#if TARGET_64BIT
- case CorElementType.ELEMENT_TYPE_I:
-#endif
return Unsafe.As<byte, long>(ref pThisValue).CompareTo(Unsafe.As<byte, long>(ref pTargetValue));
case CorElementType.ELEMENT_TYPE_U8:
-#if TARGET_64BIT
- case CorElementType.ELEMENT_TYPE_U:
-#endif
return Unsafe.As<byte, ulong>(ref pThisValue).CompareTo(Unsafe.As<byte, ulong>(ref pTargetValue));
+#if RARE_ENUMS
case CorElementType.ELEMENT_TYPE_R4:
return Unsafe.As<byte, float>(ref pThisValue).CompareTo(Unsafe.As<byte, float>(ref pTargetValue));
case CorElementType.ELEMENT_TYPE_R8:
return Unsafe.As<byte, double>(ref pThisValue).CompareTo(Unsafe.As<byte, double>(ref pTargetValue));
+ case CorElementType.ELEMENT_TYPE_BOOLEAN:
+ goto case CorElementType.ELEMENT_TYPE_U1;
+
+ case CorElementType.ELEMENT_TYPE_CHAR:
+ goto case CorElementType.ELEMENT_TYPE_U2;
+
+#if TARGET_32BIT
+ case CorElementType.ELEMENT_TYPE_I:
+ goto case CorElementType.ELEMENT_TYPE_I4;
+ case CorElementType.ELEMENT_TYPE_U:
+ goto case CorElementType.ELEMENT_TYPE_U4;
+#else
+ case CorElementType.ELEMENT_TYPE_I:
+ goto case CorElementType.ELEMENT_TYPE_I8;
+ case CorElementType.ELEMENT_TYPE_U:
+ goto case CorElementType.ELEMENT_TYPE_U8;
+#endif
+#endif
+
default:
Debug.Fail("Unknown enum underlying type");
return 0;
[MethodImpl(MethodImplOptions.NoInlining)]
static string HandleRareTypes(RuntimeType enumType, ref byte rawData) =>
+#if RARE_ENUMS
InternalGetCorElementType(enumType) switch
{
CorElementType.ELEMENT_TYPE_R4 => ToString<float>(enumType, ref rawData),
CorElementType.ELEMENT_TYPE_BOOLEAN => ToStringBool(enumType, null, ref rawData),
_ => throw CreateUnknownEnumTypeException(),
};
+#else
+ throw CreateUnknownEnumTypeException();
+#endif
}
/// <summary>Converts the value of this instance to its equivalent string representation using the specified format.</summary>
[MethodImpl(MethodImplOptions.NoInlining)]
static string HandleRareTypes(RuntimeType enumType, char formatChar, ref byte rawData) =>
+#if RARE_ENUMS
InternalGetCorElementType(enumType) switch
{
CorElementType.ELEMENT_TYPE_R4 => ToString<float>(enumType, formatChar, ref rawData),
CorElementType.ELEMENT_TYPE_BOOLEAN => ToStringBool(enumType, formatChar.ToString(), ref rawData),
_ => throw CreateUnknownEnumTypeException(),
};
+#else
+ throw CreateUnknownEnumTypeException();
+#endif
}
/// <summary>This method overload is obsolete; use <see cref="ToString()"/>.</summary>
CorElementType.ELEMENT_TYPE_U4 => ToString<uint>(rtType, formatChar, ref rawData),
CorElementType.ELEMENT_TYPE_I8 => ToString<long>(rtType, formatChar, ref rawData),
CorElementType.ELEMENT_TYPE_U8 => ToString<ulong>(rtType, formatChar, ref rawData),
+#if RARE_ENUMS
CorElementType.ELEMENT_TYPE_R4 => ToString<float>(rtType, formatChar, ref rawData),
CorElementType.ELEMENT_TYPE_R8 => ToString<double>(rtType, formatChar, ref rawData),
CorElementType.ELEMENT_TYPE_I => ToString<nint>(rtType, formatChar, ref rawData),
CorElementType.ELEMENT_TYPE_U => ToString<nuint>(rtType, formatChar, ref rawData),
CorElementType.ELEMENT_TYPE_CHAR => ToString<char>(rtType, formatChar, ref rawData),
CorElementType.ELEMENT_TYPE_BOOLEAN => ToStringBool(rtType, format, ref rawData),
+#endif
_ => throw CreateUnknownEnumTypeException(),
};
}
CorElementType.ELEMENT_TYPE_U4 => TryFormatPrimitiveDefault(enumType, Unsafe.As<byte, uint>(ref rawData), destination, out charsWritten),
CorElementType.ELEMENT_TYPE_I8 => TryFormatPrimitiveDefault(enumType, Unsafe.As<byte, long>(ref rawData), destination, out charsWritten),
CorElementType.ELEMENT_TYPE_U8 => TryFormatPrimitiveDefault(enumType, Unsafe.As<byte, ulong>(ref rawData), destination, out charsWritten),
+#if RARE_ENUMS
CorElementType.ELEMENT_TYPE_R4 => TryFormatPrimitiveDefault(enumType, Unsafe.As<byte, float>(ref rawData), destination, out charsWritten),
CorElementType.ELEMENT_TYPE_R8 => TryFormatPrimitiveDefault(enumType, Unsafe.As<byte, double>(ref rawData), destination, out charsWritten),
CorElementType.ELEMENT_TYPE_I => TryFormatPrimitiveDefault(enumType, Unsafe.As<byte, nint>(ref rawData), destination, out charsWritten),
CorElementType.ELEMENT_TYPE_U => TryFormatPrimitiveDefault(enumType, Unsafe.As<byte, nuint>(ref rawData), destination, out charsWritten),
CorElementType.ELEMENT_TYPE_CHAR => TryFormatPrimitiveDefault(enumType, Unsafe.As<byte, char>(ref rawData), destination, out charsWritten),
CorElementType.ELEMENT_TYPE_BOOLEAN => TryFormatBool(enumType, rawData != 0, destination, out charsWritten, format),
+#endif
_ => throw CreateUnknownEnumTypeException(),
};
}
CorElementType.ELEMENT_TYPE_U4 => TryFormatPrimitiveNonDefault(enumType, Unsafe.As<byte, uint>(ref rawData), destination, out charsWritten, format),
CorElementType.ELEMENT_TYPE_I8 => TryFormatPrimitiveNonDefault(enumType, Unsafe.As<byte, long>(ref rawData), destination, out charsWritten, format),
CorElementType.ELEMENT_TYPE_U8 => TryFormatPrimitiveNonDefault(enumType, Unsafe.As<byte, ulong>(ref rawData), destination, out charsWritten, format),
+#if RARE_ENUMS
CorElementType.ELEMENT_TYPE_R4 => TryFormatPrimitiveNonDefault(enumType, Unsafe.As<byte, float>(ref rawData), destination, out charsWritten, format),
CorElementType.ELEMENT_TYPE_R8 => TryFormatPrimitiveNonDefault(enumType, Unsafe.As<byte, double>(ref rawData), destination, out charsWritten, format),
CorElementType.ELEMENT_TYPE_I => TryFormatPrimitiveNonDefault(enumType, Unsafe.As<byte, nint>(ref rawData), destination, out charsWritten, format),
CorElementType.ELEMENT_TYPE_U => TryFormatPrimitiveNonDefault(enumType, Unsafe.As<byte, nuint>(ref rawData), destination, out charsWritten, format),
CorElementType.ELEMENT_TYPE_CHAR => TryFormatPrimitiveNonDefault(enumType, Unsafe.As<byte, char>(ref rawData), destination, out charsWritten, format),
CorElementType.ELEMENT_TYPE_BOOLEAN => TryFormatBool(enumType, rawData != 0, destination, out charsWritten, format),
+#endif
_ => throw CreateUnknownEnumTypeException(),
};
}
if (underlyingType == typeof(sbyte)) return TryFormatPrimitiveDefault(rt, *(sbyte*)&value, destination, out charsWritten);
if (underlyingType == typeof(short)) return TryFormatPrimitiveDefault(rt, *(short*)&value, destination, out charsWritten);
if (underlyingType == typeof(ushort)) return TryFormatPrimitiveDefault(rt, *(ushort*)&value, destination, out charsWritten);
+#if RARE_ENUMS
if (underlyingType == typeof(nint)) return TryFormatPrimitiveDefault(rt, *(nint*)&value, destination, out charsWritten);
if (underlyingType == typeof(nuint)) return TryFormatPrimitiveDefault(rt, *(nuint*)&value, destination, out charsWritten);
- if (underlyingType == typeof(char)) return TryFormatPrimitiveDefault(rt, *(char*)&value, destination, out charsWritten);
if (underlyingType == typeof(float)) return TryFormatPrimitiveDefault(rt, *(float*)&value, destination, out charsWritten);
if (underlyingType == typeof(double)) return TryFormatPrimitiveDefault(rt, *(double*)&value, destination, out charsWritten);
+ if (underlyingType == typeof(char)) return TryFormatPrimitiveDefault(rt, *(char*)&value, destination, out charsWritten);
if (underlyingType == typeof(bool)) return TryFormatBool(rt, *(bool*)&value, destination, out charsWritten, format: default);
+#endif
}
else
{
if (underlyingType == typeof(sbyte)) return TryFormatPrimitiveNonDefault(rt, *(sbyte*)&value, destination, out charsWritten, format);
if (underlyingType == typeof(short)) return TryFormatPrimitiveNonDefault(rt, *(short*)&value, destination, out charsWritten, format);
if (underlyingType == typeof(ushort)) return TryFormatPrimitiveNonDefault(rt, *(ushort*)&value, destination, out charsWritten, format);
+#if RARE_ENUMS
if (underlyingType == typeof(nint)) return TryFormatPrimitiveNonDefault(rt, *(nint*)&value, destination, out charsWritten, format);
if (underlyingType == typeof(nuint)) return TryFormatPrimitiveNonDefault(rt, *(nuint*)&value, destination, out charsWritten, format);
- if (underlyingType == typeof(char)) return TryFormatPrimitiveNonDefault(rt, *(char*)&value, destination, out charsWritten, format);
if (underlyingType == typeof(float)) return TryFormatPrimitiveNonDefault(rt, *(float*)&value, destination, out charsWritten, format);
if (underlyingType == typeof(double)) return TryFormatPrimitiveNonDefault(rt, *(double*)&value, destination, out charsWritten, format);
+ if (underlyingType == typeof(char)) return TryFormatPrimitiveNonDefault(rt, *(char*)&value, destination, out charsWritten, format);
if (underlyingType == typeof(bool)) return TryFormatBool(rt, *(bool*)&value, destination, out charsWritten, format);
+#endif
}
throw CreateUnknownEnumTypeException();
if (underlyingType == typeof(sbyte)) return TryFormatPrimitiveDefault(rt, *(sbyte*)&value, destination, out charsWritten);
if (underlyingType == typeof(short)) return TryFormatPrimitiveDefault(rt, *(short*)&value, destination, out charsWritten);
if (underlyingType == typeof(ushort)) return TryFormatPrimitiveDefault(rt, *(ushort*)&value, destination, out charsWritten);
+#if RARE_ENUMS
if (underlyingType == typeof(nint)) return TryFormatPrimitiveDefault(rt, *(nint*)&value, destination, out charsWritten);
if (underlyingType == typeof(nuint)) return TryFormatPrimitiveDefault(rt, *(nuint*)&value, destination, out charsWritten);
- if (underlyingType == typeof(char)) return TryFormatPrimitiveDefault(rt, *(char*)&value, destination, out charsWritten);
if (underlyingType == typeof(float)) return TryFormatPrimitiveDefault(rt, *(float*)&value, destination, out charsWritten);
if (underlyingType == typeof(double)) return TryFormatPrimitiveDefault(rt, *(double*)&value, destination, out charsWritten);
+ if (underlyingType == typeof(char)) return TryFormatPrimitiveDefault(rt, *(char*)&value, destination, out charsWritten);
if (underlyingType == typeof(bool)) return TryFormatBool(rt, *(bool*)&value, destination, out charsWritten, format: default);
+#endif
}
else
{
if (underlyingType == typeof(sbyte)) return TryFormatPrimitiveNonDefault(rt, *(sbyte*)&value, destination, out charsWritten, format);
if (underlyingType == typeof(short)) return TryFormatPrimitiveNonDefault(rt, *(short*)&value, destination, out charsWritten, format);
if (underlyingType == typeof(ushort)) return TryFormatPrimitiveNonDefault(rt, *(ushort*)&value, destination, out charsWritten, format);
+#if RARE_ENUMS
if (underlyingType == typeof(nint)) return TryFormatPrimitiveNonDefault(rt, *(nint*)&value, destination, out charsWritten, format);
if (underlyingType == typeof(nuint)) return TryFormatPrimitiveNonDefault(rt, *(nuint*)&value, destination, out charsWritten, format);
- if (underlyingType == typeof(char)) return TryFormatPrimitiveNonDefault(rt, *(char*)&value, destination, out charsWritten, format);
if (underlyingType == typeof(float)) return TryFormatPrimitiveNonDefault(rt, *(float*)&value, destination, out charsWritten, format);
if (underlyingType == typeof(double)) return TryFormatPrimitiveNonDefault(rt, *(double*)&value, destination, out charsWritten, format);
+ if (underlyingType == typeof(char)) return TryFormatPrimitiveNonDefault(rt, *(char*)&value, destination, out charsWritten, format);
if (underlyingType == typeof(bool)) return TryFormatBool(rt, *(bool*)&value, destination, out charsWritten, format);
+#endif
}
throw CreateUnknownEnumTypeException();
case TypeCode.Byte: return ToObject(enumType, (byte)value);
case TypeCode.UInt16: return ToObject(enumType, (ushort)value);
case TypeCode.UInt64: return ToObject(enumType, (ulong)value);
- case TypeCode.Char: return ToObject(enumType, (char)value);
- case TypeCode.Boolean: return ToObject(enumType, (bool)value ? 1L : 0L);
case TypeCode.Single: return ToObject(enumType, BitConverter.SingleToInt32Bits((float)value));
case TypeCode.Double: return ToObject(enumType, BitConverter.DoubleToInt64Bits((double)value));
+ case TypeCode.Char: return ToObject(enumType, (char)value);
+ case TypeCode.Boolean: return ToObject(enumType, (bool)value ? 1L : 0L);
};
Type valueType = value.GetType();
yield return new object[] { ulong.MinValue.ToString(), false, (UInt64Enum)ulong.MinValue };
yield return new object[] { ulong.MaxValue.ToString(), false, (UInt64Enum)ulong.MaxValue };
- if (PlatformDetection.IsReflectionEmitSupported)
+ if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
{
// Char
yield return new object[] { "Value1", false, Enum.ToObject(s_charEnumType, (char)1) };
yield return new object[] { typeof(UInt64Enum), "-1", false, typeof(OverflowException) };
yield return new object[] { typeof(UInt64Enum), "18446744073709551616", false, typeof(OverflowException) };
- if (PlatformDetection.IsReflectionEmitSupported)
+ if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
{
// Char
yield return new object[] { s_charEnumType, ((char)1).ToString(), false, typeof(ArgumentException) };
yield return new object[] { (char)4, null };
}
- [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
[MemberData(nameof(GetName_CharEnum_TestData))]
public void GetName_InvokeCharEnum_ReturnsExpected(object value, string expected)
{
yield return new object[] { false, "Value2" };
}
- [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
[MemberData(nameof(GetName_BoolEnum_TestData))]
public void GetName_InvokeBoolEnum_ReturnsExpected(object value, string expected)
{
yield return new object[] { (char)99, false };
}
- [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
[MemberData(nameof(IsDefined_CharEnum_TestData))]
public void IsDefined_InvokeCharEnum_ReturnsExpected(object value, bool expected)
{
yield return new object[] { false, true };
}
- [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
[MemberData(nameof(IsDefined_BoolEnum_TestData))]
public void IsDefined_InvokeBoolEnum_ReturnsExpected(object value, bool expected)
{
yield return new object[] { (UInt64Enum)0x3f06, (UInt64Enum)0x0010, false };
yield return new object[] { (UInt64Enum)0x3f06, (UInt64Enum)0x3f16, false };
- if (PlatformDetection.IsReflectionEmitSupported)
+ if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
{
// Char
yield return new object[] { Enum.Parse(s_charEnumType, "Value0x3f06"), Enum.Parse(s_charEnumType, "Value0x3000"), true };
yield return new object[] { typeof(UInt64Enum), (ulong)77, (UInt64Enum)77 };
yield return new object[] { typeof(UInt64Enum), (ulong)0x0123456789abcdefL, (UInt64Enum)0x0123456789abcdefL };
- if (PlatformDetection.IsReflectionEmitSupported)
+ if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
{
// Char
yield return new object[] { s_charEnumType, (char)1, Enum.Parse(s_charEnumType, "Value1") };
yield return new object[] { typeof(Enum), typeof(ArgumentException) };
yield return new object[] { typeof(object), typeof(ArgumentException) };
- if (PlatformDetection.IsReflectionEmitSupported)
+ if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
yield return new object[] { GetNonRuntimeEnumTypeBuilder(typeof(int)), typeof(ArgumentException) };
}
yield return new object[] { typeof(SimpleEnum), null, typeof(ArgumentNullException) };
yield return new object[] { typeof(SimpleEnum), "Hello", typeof(ArgumentException) };
- if (PlatformDetection.IsReflectionEmitSupported)
+ if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
{
yield return new object[] { s_intPtrEnumType, (IntPtr)1, typeof(ArgumentException) };
yield return new object[] { s_uintPtrEnumType, (UIntPtr)1, typeof(ArgumentException) };
yield return new object[] { UInt64Enum.One, new object(), false };
yield return new object[] { UInt64Enum.One, null, false };
- if (PlatformDetection.IsReflectionEmitSupported)
+ if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
{
// Char
yield return new object[] { Enum.Parse(s_charEnumType, "Value1"), Enum.Parse(s_charEnumType, "Value1"), true };
yield return new object[] { UInt64Enum.One, UInt64Enum.Max, -1 };
yield return new object[] { UInt64Enum.One, null, 1 };
- if (PlatformDetection.IsReflectionEmitSupported)
+ if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
{
// Char
yield return new object[] { Enum.Parse(s_charEnumType, "Value2"), Enum.Parse(s_charEnumType, "Value2"), 0 };
yield return new object[] { typeof(Int64Enum), typeof(long) };
yield return new object[] { typeof(UInt64Enum), typeof(ulong) };
- if (PlatformDetection.IsReflectionEmitSupported)
+ if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
{
yield return new object[] { s_charEnumType, typeof(char) };
yield return new object[] { s_boolEnumType, typeof(bool) };
Assert.Equal(expected, Enum.GetNames<UInt64Enum>());
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
public void GetNames_InvokeCharEnum_ReturnsExpected()
{
var expected = new string[] { "Value0x0000", "Value1", "Value2", "Value0x0010", "Value0x0f06", "Value0x1000", "Value0x3000", "Value0x3f06", "Value0x3f16" };
Assert.NotSame(Enum.GetNames(s_charEnumType), Enum.GetNames(s_charEnumType));
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
public void GetNames_InvokeBoolEnum_ReturnsExpected()
{
var expected = new string[] { "Value2", "Value1" };
Assert.NotSame(Enum.GetNames(s_boolEnumType), Enum.GetNames(s_boolEnumType));
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
public void GetNames_InvokeSingleEnum_ReturnsExpected()
{
var expected = new string[] { "Value0x0000", "Value1", "Value2", "Value0x0010", "Value0x0f06", "Value0x1000", "Value0x3000", "Value0x3f06", "Value0x3f16" };
Assert.NotSame(Enum.GetNames(s_floatEnumType), Enum.GetNames(s_floatEnumType));
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
public void GetNames_InvokeDoubleEnum_ReturnsExpected()
{
var expected = new string[] { "Value0x0000", "Value1", "Value2", "Value0x0010", "Value0x0f06", "Value0x1000", "Value0x3000", "Value0x3f06", "Value0x3f16" };
Assert.NotSame(Enum.GetNames(s_doubleEnumType), Enum.GetNames(s_doubleEnumType));
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
public void GetNames_InvokeIntPtrEnum_ReturnsExpected()
{
var expected = new string[0];
Assert.Same(Enum.GetNames(s_intPtrEnumType), Enum.GetNames(s_intPtrEnumType));
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
public void GetNames_InvokeUIntPtrEnum_ReturnsExpected()
{
var expected = new string[0];
Assert.Equal(expected, Enum.GetValues<UInt64Enum>());
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
public void GetValues_InvokeCharEnum_ReturnsExpected()
{
var expected = new object[] { Enum.Parse(s_charEnumType, "Value0x0000"), Enum.Parse(s_charEnumType, "Value1"), Enum.Parse(s_charEnumType, "Value2"), Enum.Parse(s_charEnumType, "Value0x0010"), Enum.Parse(s_charEnumType, "Value0x0f06"), Enum.Parse(s_charEnumType, "Value0x1000"), Enum.Parse(s_charEnumType, "Value0x3000"), Enum.Parse(s_charEnumType, "Value0x3f06"), Enum.Parse(s_charEnumType, "Value0x3f16") };
Assert.NotSame(Enum.GetValues(s_charEnumType), Enum.GetValues(s_charEnumType));
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
public void GetValues_InvokeBoolEnum_ReturnsExpected()
{
var expected = new object[] { Enum.Parse(s_boolEnumType, "Value2"), Enum.Parse(s_boolEnumType, "Value1") };
Assert.NotSame(Enum.GetValues(s_boolEnumType), Enum.GetValues(s_boolEnumType));
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
public void GetValues_InvokeSingleEnum_ReturnsExpected()
{
var expected = new object[] { Enum.Parse(s_floatEnumType, "Value0x0000"), Enum.Parse(s_floatEnumType, "Value1"), Enum.Parse(s_floatEnumType, "Value2"), Enum.Parse(s_floatEnumType, "Value0x0010"), Enum.Parse(s_floatEnumType, "Value0x0f06"), Enum.Parse(s_floatEnumType, "Value0x1000"), Enum.Parse(s_floatEnumType, "Value0x3000"), Enum.Parse(s_floatEnumType, "Value0x3f06"), Enum.Parse(s_floatEnumType, "Value0x3f16") };
Assert.NotSame(Enum.GetValues(s_floatEnumType), Enum.GetValues(s_floatEnumType));
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
public void GetValues_InvokeDoubleEnum_ReturnsExpected()
{
var expected = new object[] { Enum.Parse(s_doubleEnumType, "Value0x0000"), Enum.Parse(s_doubleEnumType, "Value1"), Enum.Parse(s_doubleEnumType, "Value2"), Enum.Parse(s_doubleEnumType, "Value0x0010"), Enum.Parse(s_doubleEnumType, "Value0x0f06"), Enum.Parse(s_doubleEnumType, "Value0x1000"), Enum.Parse(s_doubleEnumType, "Value0x3000"), Enum.Parse(s_doubleEnumType, "Value0x3f06"), Enum.Parse(s_doubleEnumType, "Value0x3f16") };
Assert.NotSame(Enum.GetValues(s_doubleEnumType), Enum.GetValues(s_doubleEnumType));
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
public void GetValues_InvokeIntPtrEnum_ReturnsExpected()
{
var expected = new object[0];
Assert.NotSame(Enum.GetValues(s_intPtrEnumType), Enum.GetValues(s_intPtrEnumType));
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
public void GetValues_InvokeUIntPtrEnum_ReturnsExpected()
{
var expected = new object[0];
yield return new object[] { (UInt64Enum)99, "D", "99" };
yield return new object[] { UInt64Enum.Max, "D", "18446744073709551615" };
- if (PlatformDetection.IsReflectionEmitSupported)
+ if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
{
// "D": Char
yield return new object[] { Enum.ToObject(s_charEnumType, (char)0), "D", ((char)0).ToString() };
yield return new object[] { (UInt64Enum)99, "X", "0000000000000063" };
yield return new object[] { UInt64Enum.Max, "X", "FFFFFFFFFFFFFFFF" };
- if (PlatformDetection.IsReflectionEmitSupported)
+ if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
{
// "X": Char
yield return new object[] { Enum.ToObject(s_charEnumType, (char)0), "X", "0000" };
yield return new object[] { (UInt64Enum)5, "F", "5" };
yield return new object[] { UInt64Enum.Max, "F", "Max" };
- if (PlatformDetection.IsReflectionEmitSupported)
+ if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
{
// "F": Char
yield return new object[] { Enum.ToObject(s_charEnumType, (char)1), "F", "Value1" };
// "F": Flags Attribute
yield return new object[] { AttributeTargets.Class | AttributeTargets.Delegate, "F", "Class, Delegate" };
- if (PlatformDetection.IsReflectionEmitSupported)
+ if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
{
// "G": Char
yield return new object[] { Enum.ToObject(s_charEnumType, char.MaxValue), "G", char.MaxValue.ToString() };
yield return new object[] { typeof(AttributeTargets), (int)(AttributeTargets.Class | AttributeTargets.Delegate), "G", "Class, Delegate" };
// nint/nuint types
- if (PlatformDetection.IsReflectionEmitSupported)
+ if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
{
yield return new object[] { s_intPtrEnumType, (nint)1, "G", "1" };
yield return new object[] { s_uintPtrEnumType, (nuint)2, "F", "2" };
yield return new object[] { Enum.ToObject(s_doubleEnumType, 2) };
}
- [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+ [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
[MemberData(nameof(UnsupportedEnum_TestData))]
public static void ToString_UnsupportedEnumType_ThrowsArgumentException(Enum e)
{
private static EnumBuilder GetNonRuntimeEnumTypeBuilder(Type underlyingType)
{
- if (!PlatformDetection.IsReflectionEmitSupported)
+ if (!PlatformDetection.IsReflectionEmitSupported || !PlatformDetection.IsRareEnumsSupported)
return null;
AssemblyBuilder assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("Name"), AssemblyBuilderAccess.Run);