-
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
Add EnumMember API #28198
Comments
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 The |
Thanks for the consideration. |
Ha, my spider senses told me we already had the API :-)
Correct |
Split off from #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 #20008.
Updates
GetMembers
to return anIReadOnlyList
instead ofIEnumerable
.The text was updated successfully, but these errors were encountered: