Document breaking change: Replace DAMT.All with restricted annotations on reflection APIs #49415
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR documents a breaking change introduced in .NET 10.0 where the
System.ReflectionAPIsInvokeMember,FindMembers, andDeclaredMembersnow use more restricted annotations instead of the overly permissiveDAMT.All.Overview
The previous use of
DAMT.Allannotation on these reflection APIs could lead to unintended behavior, such as capturing interface methods implemented by a class or generating warnings due to unsafe reflection calls. This change improves accuracy of annotations and ensures better compatibility with trimming and reflection scenarios.Impact
This is both a source incompatible and behavioral breaking change that affects developers who:
IReflectinterfaceSystem.Reflection.TypeInfoDevelopers will need to update their code to use more restricted annotations such as
DynamicallyAccessedMemberTypes.PublicMethods,DynamicallyAccessedMemberTypes.NonPublicMethods, or other appropriate types instead ofDAMT.All.Changes in this PR
/docs/core/compatibility/reflection/10/damt-all-restricted-annotations.mdwith comprehensive documentation including:docs/core/compatibility/toc.yml) under the Reflection sectiondocs/core/compatibility/10.0.md) under Core .NET libraries sectionValidation
All changes pass markdownlint validation and follow Microsoft documentation style guidelines.
Fixes #<issue_number>
Original prompt
This section details on the original issue you should resolve
<issue_title>[Breaking change]: Replace DAMT.All with more restricted annotation on InvokeMember/FindMembers/DeclaredMembers</issue_title>
<issue_description>### [Breaking change]: Replace DAMT.All with more restricted annotation on InvokeMember/FindMembers/DeclaredMembers
Description
Starting in .NET 10.0, the
System.ReflectionAPIsInvokeMember,FindMembers, andDeclaredMembershave been updated to use more restricted annotations instead ofDAMT.All. This change affects scenarios where developers implement theIReflectinterface or derive fromSystem.TypeInfo. The previous use ofDAMT.Allwas overly permissive and could lead to unintended behavior, such as capturing interface methods implemented by a class or generating warnings due to unsafe reflection calls.This change improves the accuracy of annotations and ensures better compatibility with trimming and reflection scenarios. However, it introduces a breaking change for developers who have implemented
IReflector derived fromSystem.TypeInfo, as they may need to update their annotations to align with the new behavior.Version
.NET 10.0
Previous behavior
The
InvokeMember,FindMembers, andDeclaredMembersAPIs used theDAMT.Allannotation, which was overly permissive. This could result in capturing additional members, such as interface methods implemented by a class, and potentially cause runtime warnings or unsafe reflection calls.For example:
New behavior
The
InvokeMember,FindMembers, andDeclaredMembersAPIs now use more restricted annotations, which provide better control over the members captured during reflection. Developers implementingIReflector deriving fromSystem.TypeInfomust update their annotations to match the new behavior.For example:
Type of breaking change
Reason for change
The change was introduced to improve the accuracy of annotations in
System.ReflectionAPIs and to address issues caused by the overly permissiveDAMT.Allannotation. This ensures better compatibility with trimming and reflection scenarios, reduces runtime warnings, and prevents unsafe reflection calls.Recommended action
Developers who implement
IReflector derive fromSystem.TypeInfoshould review their code and update annotations to align with the new behavior. Specifically:DAMT.Allannotations with more restricted annotations, such asDynamicallyAccessedMemberTypes.PublicMethods,DynamicallyAccessedMemberTypes.NonPublicMethods, or other appropriate types.For example, update code as follows:
Before:
After:
Feature area
Affected APIs
System.Reflection.IReflect.InvokeMemberSystem.Reflection.IReflect.FindMembersSystem.Reflection.TypeInfo.DeclaredMembersAdditional resources
For more information on
DynamicallyAccessedMembersannotations and their usage, refer to the Microsoft documentation on trimming and reflection.Finally, please email a link to this breaking change issue to .NET Breaking Change Notifications.</issue_description>
Comments on the Issue (you are @copilot in this section)
Fixes #49414
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.