SmartEnum icon indicating copy to clipboard operation
SmartEnum copied to clipboard

NativeAOT and trimming support

Open tompipe opened this issue 1 year ago • 2 comments

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:

https://github.com/ardalis/SmartEnum/blob/0eae38b8e3273440ab07e8e815ba01436aa1f07a/src/SmartEnum/SmartEnum.cs#L70-L79

https://github.com/ardalis/SmartEnum/blob/0eae38b8e3273440ab07e8e815ba01436aa1f07a/src/SmartEnum/TypeExtensions.cs#L10-L16

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?

tompipe avatar Sep 17 '24 18:09 tompipe

Source generators may be the best option for replacing the reflection code. I too find tremendous value in this library and have a need for AOT compatibility.

zbarrier avatar Apr 21 '25 15:04 zbarrier

Definitely looking into it

ardalis avatar Apr 21 '25 20:04 ardalis