Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new API GetEnumValuesAsUnderlyingType #73057

Merged
merged 18 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Enum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,30 @@ public static Array GetValues(Type enumType)
return enumType.GetEnumValues();
}

/// <summary>
/// Gets an array of values of the underlying type of the Enum.
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <typeparam name="TEnum">Enum type</typeparam>
/// /// <remarks>
/// This method can be used to get enum values when creating an Array of Enum is challenging
/// For example, reflection-only context or on a platform where runtime codegen is not available.
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
/// </remarks>
/// <returns>Array of values containing the underlying type of the Enum</returns>
public static Array GetValuesAsUnderlyingType<TEnum>() where TEnum : struct, Enum =>
typeof(TEnum).GetEnumValuesAsUnderlyingType();

/// <summary>
/// Gets an array of values of the underlying type of the Enum.
/// </summary>
/// <param name="enumType">Enum type</param>
/// <remarks>
/// This method can be used to get enum values when creating an Array of Enum is challenging
/// For example, in reflection-only context or on a platform where runtime codegen is not available.
/// </remarks>
/// <returns>Array of values containing the underlying type of the Enum</returns>
/// <exception cref="ArgumentNullException">
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
/// Thrown when the enum type is null
/// </exception>
public static Array GetValuesAsUnderlyingType(Type enumType)
{
ArgumentNullException.ThrowIfNull(enumType);
Expand Down
95 changes: 88 additions & 7 deletions src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ public override Array GetEnumValues()
return ret;
}

/// <summary>
/// Gets an array of values of the underlying type of the Enum.
/// </summary>
/// <remarks>
/// This method can be used to get enum values when creating an Array of Enum is challenging
/// For example, in reflection-only context or on a platform where runtime codegen is not available.
/// </remarks>
/// <returns>Array of values containing the underlying type of the Enum</returns>
/// <exception cref="ArgumentException">
/// Thrown when the type is not Enum
/// </exception>
public override Array GetEnumValuesAsUnderlyingType()
{
if (!IsActualEnum)
Expand All @@ -147,15 +158,85 @@ public override Array GetEnumValuesAsUnderlyingType()
// Get all of the values
ulong[] values = Enum.InternalGetValues(this);

Array ret = Array.CreateInstance(Enum.InternalGetUnderlyingType(this), values.Length);

for (int i = 0; i < values.Length; i++)
switch (RuntimeTypeHandle.GetCorElementType(Enum.InternalGetUnderlyingType(this)))
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
{
object val = Enum.ToObject(this, values[i]);
ret.SetValue(val, i);
}
case CorElementType.ELEMENT_TYPE_U1:
{
var ret = new byte[values.Length];
for (int i = 0; i < values.Length; i++)
{
ret[i] = (byte)values[i];
}
return ret;
}

return ret;
case CorElementType.ELEMENT_TYPE_U2:
{
var ret = new ushort[values.Length];
for (int i = 0; i < values.Length; i++)
{
ret[i] = (ushort)values[i];
}
return ret;
}

case CorElementType.ELEMENT_TYPE_U4:
{
var ret = new uint[values.Length];
for (int i = 0; i < values.Length; i++)
{
ret[i] = (uint)values[i];
}
return ret;
}

case CorElementType.ELEMENT_TYPE_U8:
{
return (Array)values.Clone();
}

case CorElementType.ELEMENT_TYPE_I1:
{
var ret = new sbyte[values.Length];
for (int i = 0; i < values.Length; i++)
{
ret[i] = (sbyte)values[i];
}
return ret;
}

case CorElementType.ELEMENT_TYPE_I2:
{
var ret = new short[values.Length];
for (int i = 0; i < values.Length; i++)
{
ret[i] = (short)values[i];
}
return ret;
}

case CorElementType.ELEMENT_TYPE_I4:
{
var ret = new int[values.Length];
for (int i = 0; i < values.Length; i++)
{
ret[i] = (int)values[i];
}
return ret;
}

case CorElementType.ELEMENT_TYPE_I8:
{
var ret = new long[values.Length];
for (int i = 0; i < values.Length; i++)
{
ret[i] = (long)values[i];
}
return ret;
}
default:
throw new NotImplementedException("Not Implemented");
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
}
}

public override Type GetEnumUnderlyingType()
Expand Down
11 changes: 11 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,17 @@ public virtual Array GetEnumValues()
throw NotImplemented.ByDesign;
}

/// <summary>
/// Gets an array of values of the underlying type of the Enum.
/// </summary>
/// <remarks>
/// This method can be used to get enum values when creating an Array of Enum is challenging
/// For example, in reflection-only context or on a platform where runtime codegen is not available.
/// </remarks>
/// <returns>Array of values containing the underlying type of the Enum</returns>
/// <exception cref="ArgumentException">
/// Thrown when the type is not Enum
/// </exception>
public virtual Array GetEnumValuesAsUnderlyingType() => throw new NotSupportedException(SR.NotSupported_SubclassOverride);

[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
Expand Down