-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Consider adding a new reflection API to get field values of an Enum #100592
Comments
Can't NativeAOT not generate reflection info for such 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: public class Enum
{
[UnconditionalSuppressMessage("Trimming", "IL2075:", Justification = "Trimmer does not trim Enums")]
public static FieldInfo[] GetValuesAsFieldInfos(Type enumType)
{
ArgumentNullException.ThrowIfNull(enumType);
if (!enumType.IsEnum)
throw new ArgumentException(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. |
Here's another use case: There we need to get the |
The case in EnumConverter doesn't directly expose the However, the case in EventSource could use Moving to V10; priority appears low here (removing a couple cases of UnconditionalSuppressMessage). |
Here's one more hit on the same scenario: Getting the |
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.
The text was updated successfully, but these errors were encountered: