-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: Type.GetEnumValuesAsUnderlyingType #72498
Comments
Tagging subscribers to this area: @dotnet/area-system-reflection Issue DetailsBackground and motivationThe existing
It would be good to have API that can work in these contexts by returning an array of the underlying type (i.e. API Proposalnamespace System;
public abstract class Type
{
public virtual Array GetEnumValuesAsUnderlyingType();
} API Usageforeach (var value in typeof(SomeEnum).GetEnumValuesAsUnderlyingType())
Console.WriteLine(value);
// Prints:
// 0
// 1
//
// As opposed to:
//
// SomeEnumValue1
// SomeEnumValue2 Alternative DesignsAllow implementers to not satisfy the contractThe implementation of the existing Write more codeCan be expressed in a less performant and more verbose way already:
RisksNo response
|
Would this API return an array of the underlying type in all cases or only when it's not possible to figure out the type under AOT? |
In all cases. Switching the array type around would be a compat problem in the same sense as returning |
Does it make more sense to have the API on Type or in Enum? Currently APIs are in both of those places. |
For the I'll leave it up to the API review board whether it makes sense to add a shortcut on |
@MichalStrehovsky This is marked blocking. Be sure to add a milestone (7.0 or Future) as well. Thanks! |
Marked for 7.0, thanks for spotting! |
/cc @lambdageek |
namespace System;
public partial class Type
{
// Existing enum related methods:
//
// public virtual string? GetEnumName(Object);
// public virtual string[] GetEnumNames();
// public virtual Type GetEnumUnderlyingType();
// public virtual object[] GetEnumValues();
public virtual Array GetEnumValuesAsUnderlyingType();
}
public partial class Enum
{
public static Array GetValuesAsUnderlyingType<TEnum>() where TEnum: struct, Enum;
public static Array GetValuesAsUnderlyingType(Type type);
} |
Background and motivation
The existing
Type.GetEnumValues
API returns aSystem.Array
that is of the given enum type (i.e.SomeEnum[]
when called ontypeof(SomeEnum)
). This makes implementing the API challenging when the enum array cannot be created. There are multiple reasons why the enum array might not be possible to create:MetadataLoadContext
:runtime/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/Types/RoType.cs
Line 289 in d6e8686
It would be good to have API that can work in these contexts by returning an array of the underlying type (i.e.
int[]
fortypeof(SomeInt32Enum)
.API Proposal
API Usage
Alternative Designs
Allow implementers to not satisfy the contract
The implementation of the existing
Type.GetEnumValues
could just return the underlying array directly since the signature is justSystem.Array
. But that is probably pretty breaking - see #72236 (comment) for samples.Write more code
Can be expressed in a less performant and more verbose way already:
Risks
No response
The text was updated successfully, but these errors were encountered: