aws / aws/aws-dotnet-messaging
Type parameters should be decorated with DynamicallyAccessedMembersAttribute
- Dominant language
- C#
- Stars
- 143
- Forks
- 27
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 4
Description
### 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](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembersattribute?view=net-8.0) 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:
```cs
public static IServiceCollection AddSingleton(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
- [X] 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
Contributor guide
Research direction
Locate the MessageBusBuilder.AddMessageHandler entry point and inspect its other methods that accept Type instances or dynamically constructed type parameters. Compare the proposed annotations with the ASP.NET Core AddSingleton, AddScoped, and AddTransient examples, then verify that every relevant API carries the appropriate public-constructor annotation and that trimming or Native AOT analysis recognizes the types.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100