-
-
Notifications
You must be signed in to change notification settings - Fork 182
Description
Firstly, let me open with my thanks for this library, I've only recently discovered it, but wish I'd found it earlier! 😄
I've had a minor issue in the project I'm working on, which is being compiled for native AOT. The following warning is displayed in VS:
Assembly 'Ardalis.SmartEnum' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries
And it didn't work as expected, due to my smart enum not being able to obtain values using the TryFromValue
method.
I had a quick look at the source, and I suspected this was due to the use of reflection:
SmartEnum/src/SmartEnum/SmartEnum.cs
Lines 70 to 79 in 0eae38b
private static TEnum[] GetAllOptions() | |
{ | |
Type baseType = typeof(TEnum); | |
return Assembly.GetAssembly(baseType) | |
.GetTypes() | |
.Where(t => baseType.IsAssignableFrom(t)) | |
.SelectMany(t => t.GetFieldsOfType<TEnum>()) | |
.OrderBy(t => t.Name) | |
.ToArray(); | |
} |
SmartEnum/src/SmartEnum/TypeExtensions.cs
Lines 10 to 16 in 0eae38b
public static List<TFieldType> GetFieldsOfType<TFieldType>(this Type type) | |
{ | |
return type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) | |
.Where(p => type.IsAssignableFrom(p.FieldType)) | |
.Select(pi => (TFieldType)pi.GetValue(null)) | |
.ToList(); | |
} |
I was successfully able to hack around it, by adding the following to my enum, providing the hints to the IL to prevent it optimizing away the info needed to perform reflection.
[RequiresUnreferencedCode("Calls System.Reflection.Assembly.GetTypes()")]
public sealed class MySmartEnum : SmartEnum<MySmartEnum, Guid>
{
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicFields, typeof(MySmartEnum))]
public new static MySmartEnum FromName(string name, bool ignoreCase = false)
{
return SmartEnum<MySmartEnum, Guid>.FromName(name, ignoreCase);
}
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicFields, typeof(MySmartEnum))]
public new static MySmartEnum FromValue(Guid value)
{
return SmartEnum<MySmartEnum, Guid>.FromValue(value);
}
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicFields, typeof(MySmartEnum))]
public new static bool TryFromValue(Guid value, out MySmartEnum result)
{
return SmartEnum<MySmartEnum, Guid>.TryFromValue(value, out result);
}
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicFields, typeof(MySmartEnum))]
public new static bool TryFromName(string name, out MySmartEnum result)
{
return SmartEnum<MySmartEnum, Guid>.TryFromName(name, out result);
}
}
Thought It would be beneficial to set these options in the source if possible, so the warnings are prevented, or at least open an issue for others to reference if they're in a similar situation.
Perhaps its also worth giving some consideration to the feasiblity of whether the use of reflection could be avoided entirely/optionally.
Could derived classes provide their own implementation of GetAllOptions, or perhaps theres another approach of handling the 'discoverability' of available options?
Could it be as simple as being 'registered' into the _enumOptions collection when being constructed?