Closed TylerBrinkley closed 4 years ago
We should add this API:
namespace System
{
public partial class Enum
{
public static T[] GetValues<T>();
}
}
So this code:
var values = (MyEnum[])Enum.GetValues(typeof(MyEnum));
var names = Enum.GetNames(typeof(MyEnum));
for (int i = 0; i < values.Length; ++i)
{
MyEnum value = values[i];
string name = names[i];
}
becomes
var values = Enum.GetValues<MyEnum>();
foreach (var value in values)
{
var name = value.ToString();
}
With respect to custom attributes, you can already do this:
FieldInfo enumField = ...;
var description = enumField.GetCustomAttributes<DescriptionAttribute>()
.SingleOrDefault()?.Description ?? "";
Generic Enum.GetValues
was approved as #2364 and added by #33589 two weeks ago.
The EnumMember
family of APIs was rejected as too high-level for System.Enum
, so there is nothing left to do. @terrajobst Is this the right conclusion?
Thanks for the consideration. Enum.GetValues<TEnum>()
was already added in https://github.com/dotnet/runtime/issues/2364 so no work to be done there. While .ToString()
doesn't handle the duplicate values case I understand if this API is too high-level here, especially including the reflection case of attributes.
Generic
Enum.GetValues
was approved as #2364 and added by #33589 two weeks ago.
Ha, my spider senses told me we already had the API :-)
The
EnumMember
family of APIs was rejected as too high-level forSystem.Enum
, so there is nothing left to do. @terrajobst Is this the right conclusion?
Correct
Split off from dotnet/runtime#20008
Rationale and Usage
Currently, to retrieve both the names and values of an enum's members requires two separate calls and requires you to use a for loop which is quite clumsy. Additionally the pattern to associate extra data with an enum member using
Attribute
s is not directly supported and instead requires users to manually retrieve theAttribute
s via reflection. This pattern is commonly used on enums using theDescriptionAttribute
,EnumMemberAttribute
, andDisplayAttribute
. There should be added direct support for the retrieval ofAttribute
s applied to enum members.What used to be this to retrieve both the names and values of an enum's members
now becomes this
And what used to be this to retrieve the
DescriptionAttribute.Description
of an enum membernow becomes this
Proposed API
API Details
This proposal makes use of a
C#
language feature that needs to be added in order for this proposal to make the most impact.This proposal specifies extension methods within
System.Enum
and as such requiresC#
to allow extension methods within non-static classes as is proposed in csharplang#301. Promoting these to extension methods later would be a breaking change due to csharplang#665 but I feel this is acceptable.Alternatively, the extension methods could be defined in a separate static
EnumExtensions
class. This is uglier but would avoid this issue and the extension methods would be available immediately instead of needing to wait for a laterC#
version to support this.This proposal stems from my work on the open source library Enums.NET.
Enum
API DetailsGetMember
retrieves theEnumMember
with the specified value or name. If there are no enum members with the specified value or namenull
is returned.GetMembers
retrieves all of theEnumMember
s of the specified enum in increasing significance bit order by their respective values.EnumMember
API DetailsEnumMember
andEnumMember<TEnum>
are a new object model formed over an enum member which include its name, value, and attributes. They only have internal constructors and there is only one instance ofEnumMember
created for each enum member thus one can useReferenceEquals
for determining equality. This also prevents allocations after the first retrieval.TEnum
inEnumMember<TEnum>
to anEnum
as it's not publicly constructible and being unconstrained would be beneficial in unconstrained generic programming.AttributeCollection
API DetailsGet
returns the firstAttribute
in the collection that is assignable to the specified attribute type if available otherwisenull
.GetAll
returns allAttribute
s in the collection that are assignable to the specified attribute type.Has
indicates if anyAttribute
s in the collection are assignable to the specified attribute type.System.ComponentModel.TypeConverter
assembly intomscorlib
while adding an automatic binding redirect.Implementation Details
A type forward would need to be added for
AttributeCollection
so that it's available from corelib. Utilizes performance improved implementation from dotnet/runtime#20008.Updates
GetMembers
to return anIReadOnlyList
instead ofIEnumerable
.