AOT: Add DynamicallyAccessedMembersAttribute
- Dominant language
- No language data
- Stars
- 4.8k
- Forks
- 6.1k
- Avg merge
- 19h 10m
- Merged PRs (30d)
- 268
Description
See [ASP.NET Core and native AOT compatibility](https://devblogs.microsoft.com/dotnet/asp-net-core-updates-in-dotnet-8-preview-3/#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]](https://learn.microsoft.com/dotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembersattribute?view=net-8.0) 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](https://learn.microsoft.com/dotnet/api/system.type.makegenerictype) rely on capabilities not available in native AOT. [Roslyn source generators](https://learn.microsoft.com/dotnet/csharp/roslyn-sdk/source-generators-overview) 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.*
* ID: 63476200-2ede-1779-9cc9-1503efec1b36
* Version Independent ID: 54aa95cb-2d5b-85c9-6c34-1fc0348f7cb1
* Content: [Native AOT deployment overview - .NET](https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/?branch=pr-en-us-36397&tabs=net7)
* Content Source: [docs/core/deploying/native-aot/index.md](https://github.com/dotnet/docs/blob/main/docs/core/deploying/native-aot/index.md)
* Product: **dotnet-fundamentals**
* GitHub Login: @LakshanF
* Microsoft Alias: **lakshanf**
Contributor guide
Assessment
This issue has not been assessed yet.