Skip to content
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

Type parameters should be decorated with DynamicallyAccessedMembersAttribute #144

Open
1 of 2 tasks
NoahStolk opened this issue Jun 3, 2024 · 2 comments
Open
1 of 2 tasks
Labels
feature-request A feature should be added or improved. p2 This is a standard priority issue queued

Comments

@NoahStolk
Copy link

NoahStolk commented Jun 3, 2024

Describe the feature

The MessageBusBuilder class exposes various methods that accept Type instances or type parameters that are used to dynamically construct instances of said types. For example, in the MessageBusBuilder.AddMessageHandler method, the THandler type is used to dynamically construct an instance of that type.

This can and should be indicated by adding DynamicallyAccessedMembersAttribute to the type parameter. This allows tools such as ReSharper to detect that this type is created dynamically. Adding this attribute would also prevent code from being trimmed/removed incorrectly when compiling to native code using Native AOT.

Use Case

Currently, ReSharper complains that my handler type is never constructed and should be removed because it is seen as "dead code". I also believe (but have not yet tested this) that my handler type would be removed if I enabled trimming or compiled my application to native code using Native AOT, which would cause runtime exceptions. There are various workarounds for this, but the obvious fix would be to just include the attribute here.

Proposed Solution

Add the attribute where necessary. Example usages of the attribute can be found in the ASP.NET Core runtime. For example, AddSingleton, AddScoped, and AddTransient use this attribute:

public static IServiceCollection AddSingleton<TService, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TImplementation>(this IServiceCollection services)
    where TService : class
    where TImplementation : class, TService
// ...

public static IServiceCollection AddSingleton(
    this IServiceCollection services,
    [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type serviceType)
// ...

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

AWS.Messaging (or related) package versions

AWS.Messaging 0.9.1

Targeted .NET Platform

.NET 8

Operating System and version

Windows 11

@NoahStolk NoahStolk added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Jun 3, 2024
@ashishdhingra ashishdhingra added needs-review and removed needs-triage This issue or PR still needs to be triaged. labels Jun 4, 2024
@ashishdhingra
Copy link
Contributor

Needs review with the team.

@ashishdhingra
Copy link
Contributor

ashishdhingra commented Jun 7, 2024

@NoahStolk Thanks for the issue report. We have a larger effort in our internal backlog to make this library NativeAOT compatible. This is one the the things we would consider. Related discussion #109.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved. p2 This is a standard priority issue queued
Projects
None yet
Development

No branches or pull requests

2 participants