You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consider adding a new reflection API to get field values of an Enum #100592
The trimmer doesn't trim Enum fields. As mentioned in the comment here, it would make sense to have a separate API that doesn't require DynamicallyAccessedMembers annotation to get Enum fields. That would prevent suppressing the warning as done here when getting Enum fields.
Note that we have added trim and AOT friendly ways to get values of an Enum: Enum.GetValuesAsUnderlyingType - #72498.
The specific case in EnumConverter wants the field values presented as FieldInfos. Fetching enum values as FieldInfos is quite inefficient, but it is required here because of pre-existing public surface.
A new API can look like this:
publicclassEnum{[UnconditionalSuppressMessage("Trimming","IL2075:",Justification="Trimmer does not trim Enums")]publicstaticFieldInfo[]GetValuesAsFieldInfos(TypeenumType){ArgumentNullException.ThrowIfNull(enumType);if(!enumType.IsEnum)thrownewArgumentException(SR.Arg_MustBeEnum,"enumType")
return enumType.GetFields(BindingFlags.Public|BindingFlags.Static);}
The question is whether fetching enum values as FieldInfos is common enough to warrant a new public API. I think we would want to see at least one or two additional cases from real-world code where this API can be used.
The specific case in EnumConverter wants the field values presented as FieldInfos. Fetching enum values as FieldInfos is quite inefficient, but it is required here because of pre-existing public surface.
The case in EnumConverter doesn't directly expose the FieldInfos but does check for [Browseable] attribute on the FieldInfo, so a new API is necessary here.
However, the case in EventSource could use Enum.GetValuesAsUnderlyingType() instead of FieldInfos as the FieldInfos used there are used temporarily + privately and do not check for the [Browseable] attribute.
Moving to V10; priority appears low here (removing a couple cases of UnconditionalSuppressMessage).
Activity
MichalPetryka commentedon Apr 3, 2024
Can't NativeAOT not generate reflection info for such fields?
jkotas commentedon Apr 3, 2024
Note that we have added trim and AOT friendly ways to get values of an Enum: Enum.GetValuesAsUnderlyingType - #72498.
The specific case in EnumConverter wants the field values presented as FieldInfos. Fetching enum values as FieldInfos is quite inefficient, but it is required here because of pre-existing public surface.
A new API can look like this:
The question is whether fetching enum values as FieldInfos is common enough to warrant a new public API. I think we would want to see at least one or two additional cases from real-world code where this API can be used.
eerhardt commentedon Apr 3, 2024
Here's another use case:
#97737
There we need to get the
EnumMemberAttributeon the Field of an Enum.steveharter commentedon Jul 22, 2024
The case in EnumConverter doesn't directly expose the
FieldInfos but does check for[Browseable]attribute on the FieldInfo, so a new API is necessary here.However, the case in EventSource could use
Enum.GetValuesAsUnderlyingType()instead of FieldInfos as the FieldInfos used there are used temporarily + privately and do not check for the[Browseable]attribute.Moving to V10; priority appears low here (removing a couple cases of UnconditionalSuppressMessage).
eerhardt commentedon Jul 22, 2024
Here's one more hit on the same scenario:
https://github.com/microsoft/OpenAPI.NET/pull/1717/files#diff-c59c9153ccf5e00142826d1c5e4d3d92470aac0bc740a302a23557432141277cR29
Getting the
DisplayAttributeof an enum field.