Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DynamicallyAccessedMembers annotations do not seem to be working on type delcarations. #113650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
eiriktsarpalis opened this issue Mar 18, 2025 · 5 comments
Labels
area-Tools-ILLink .NET linker development as well as trimming analyzers question Answer questions and provide assistance, not an issue with source code or documentation.

Comments

@eiriktsarpalis
Copy link
Member

Consider the following application:

using System.Diagnostics.CodeAnalysis;

Test(typeof(MyPoco));
static void Test(Type type) => Activator.CreateInstance(type);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
class MyPoco;

I would have expected that this would instruct the linker to preserve the default constructor for MyPoco, however when I publish to AOT I get the error:

Unhandled exception. System.MissingMethodException: No parameterless constructor defined for type 'MyPoco'.
   at System.ActivatorImplementation.CreateInstance(Type, Boolean) + 0x198
   at Program.<Main>$(String[] args) + 0x2c
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Mar 18, 2025
@eiriktsarpalis eiriktsarpalis added the linkable-framework Issues associated with delivering a linker friendly framework label Mar 18, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Mar 18, 2025
Copy link
Contributor

Tagging subscribers to 'linkable-framework': @eerhardt, @vitek-karas, @LakshanF, @sbomer, @joperezr, @marek-safar
See info in area-owners.md if you want to be subscribed.

@eiriktsarpalis eiriktsarpalis removed the linkable-framework Issues associated with delivering a linker friendly framework label Mar 18, 2025
@marek-safar marek-safar added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Mar 18, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/illink
See info in area-owners.md if you want to be subscribed.

@MichalStrehovsky
Copy link
Member

The purpose of the annotation is to make this pattern work:

// Works no matter what descendants of MyPoco
static void Test(MyPoco p) => Activator.CreateInstance(p.GetType());

We don't have DAM annotations to just keep stuff because that would just encourage suppressing warnings. DAM is always paired with enabling new warningfree scenarios. The example in top post would still produce warnings.

@MichalStrehovsky
Copy link
Member

(We delay marking things until we see the GetType call. The annotation is meaningless on it's own)

@jkotas jkotas added question Answer questions and provide assistance, not an issue with source code or documentation. and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Mar 19, 2025
@agocke
Copy link
Member

agocke commented May 2, 2025

Closing as by-design. Code with warnings has undefined behavior.

@agocke agocke closed this as completed May 2, 2025
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Tools-ILLink .NET linker development as well as trimming analyzers question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
Status: No status
Development

No branches or pull requests

5 participants