C
C#2mo ago
Sekoree

✅ Trimming/NativeAOT preserve specific constructor(s) in ClassLibrary

Hi, I'm a bit stuck with a trimming issue. I basically have a shared project that has an abstract base class that other components can implement with a constructor that has some parameters. Additionally it has a management class that handles creation of these components implementing that base class. I use the Activator class to create instances of these components (which is the issue here I guess). Is there any way to tell it not to remove the constructor on those components (and I guess that base class)? Using and rd.xml worked once, now not anymore and idk why, but telling it to not trim those libraries entirely wouldn't be nice either. Is there any way to define this from the shared project to apply to other libraries that implement that base class too? (Also open to suggestions to not have to use this (static "Create" method in a base Interface maybe(?)) The implementations look like this: The method that creates the objects: https://github.com/vocawaves/Manager/blob/2fcde9b4f24a2eb551aa11671a808c99fc64de39/Manager.Shared/ComponentManager.cs#L20 One of the Components: https://github.com/vocawaves/Manager/blob/2fcde9b4f24a2eb551aa11671a808c99fc64de39/Manager.SimplePlayer/MediaPlayer.cs#L49 The base class: https://github.com/vocawaves/Manager/blob/2fcde9b4f24a2eb551aa11671a808c99fc64de39/Manager.Shared/Interfaces/General/ManagerComponent.cs#L51
8 Replies
reflectronic
reflectronic2mo ago
to start: you should not use rd.xml. it is not officially supported, and it may be changed, broken, or removed at any time by microsoft but, anyway, if you would like to make your project AOT compatible, i strongly suggest setting <PublishAot>true</PublishAot> (or <IsAotCompatible>true</IsAotCompatible> for libaries) in your project file (or, if you just care about trimming, then PublishTrimmed/IsTrimmable) these enable trimming/AOT analysis analyzers which tell you exactly which places in your code are trim-incompatible
reflectronic
reflectronic2mo ago
for example, if I take a little piece ofCreateManagerComponent and pull it into a project with the analyzers enabled, i get this warning:
No description
reflectronic
reflectronic2mo ago
what this means is that i need to [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] to the declaration of <T>, since Activator.CreateInstance requires it. and, this tells the linker to preserve public constructors for every type which you pass as T
reflectronic
reflectronic2mo ago
and doing that resolves the warning, making this code trim-compatibile
No description
reflectronic
reflectronic2mo ago
if you resolve all of these warnings the application will be fully trim-compatible, and it will work* *you need to not just resolve the IDE warnings, but also any warnings that are generated by the linker/AOT compiler when you do dotnet publish--it does more analysis that the IDE cannot do
reflectronic
reflectronic2mo ago
there is an article that goes deeper into resolving trim warnings here https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/fixing-warnings
Introduction to trim warnings - .NET
Learn about why warnings might be produced when publishing a trimmed application, how to address them, and how to make the application "trim compatible."
reflectronic
reflectronic2mo ago
.
Sekoree
Sekoree2mo ago
Thank you so much! I think in my desperation I didn't quite enable the trimming warnings/analyzers right. Also didn't know I could put Attributes in front of the T. I currently went back to having a static Create method on the IManagerComponent interface (that I literally just committed lol), but I'm not that happy about it. Thanks again for the help, I'll play around with it a bit
Want results from more Discord servers?
Add your server
More Posts