dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.28k stars 5.91k forks source link

AOT: Add DynamicallyAccessedMembersAttribute #36399

Open Rick-Anderson opened 1 year ago

Rick-Anderson commented 1 year ago

See ASP.NET Core and native AOT compatibility

Where should the following go?

Reflection is a commonly used feature of .NET, allowing for runtime discovery and inspection of type information, dynamic invocation, and code generation. While reflection over type information is supported in native AOT, the trimming performed often can’t statically determine that a type has members that are only accessed via reflection, and are thus removed, leading to runtime exceptions. Developers can annotate their code with instructional attributes, such as [DynamicallyAccessedMembers] to indicate that members are dynamically accessed and should be left untrimmed.

Developers should also be aware that it’s not always immediately obvious that code is relying on reflection’s runtime code generation capabilities and thus can introduce native AOT incompatibilties, e.g. methods like Type.MakeGenericType rely on capabilities not available in native AOT. Roslyn source generators allow code to be generated at compile time with similar type discovery and inspection capabilities as runtime-based reflection, and are a useful alternative when preparing for native AOT compatibility.

@mitchdenny @eerhardt please recommend where this information should go.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

eerhardt commented 1 year ago

Where should the following go?

Somewhere around here, if it isn't already there.

https://learn.microsoft.com/dotnet/core/deploying/native-aot/

cc @vitek-karas @agocke @MichalStrehovsky

MichalStrehovsky commented 1 year ago

DynamicallyAccessedMember is a trimming topic and is documented around here: https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/fixing-warnings

For AOT and runtime code generation, we have this: https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/fixing-warnings