Skip to content

Enhance DynamicallyAccessedMembers Attribute to apply to class  #49465

Closed
@LakshanF

Description

@LakshanF

Background and Motivation

This proposal is taking @MichalStrehovsky's trimer proposal through its API change part. This proposal aligns well with the API proposal #49448 in enforcing the type that are required to be preserved for all derived types of a base class. #49448 helps with validating the type tree (parent type and its descendants) and this API enhancement will help with the enforcement.

The DynamicallyAccessedMembersAttribute attribute currently cannot be applied to a class or interface. Adding this support will allow developers to annotate a type to help the trimmer preserve its members that are being used. If the trimmer needs to keep the type, it will keep the required members on its derived types as well.

Proposed API

 namespace System.Diagnostics.CodeAnalysis
 {
     [AttributeUsage(
+        AttributeTargets.Class |
         AttributeTargets.Field |
         AttributeTargets.GenericParameter |
+        AttributeTargets.Interface |
+        AttributeTargets.Struct |
         AttributeTargets.Method |
         AttributeTargets.Parameter |
         AttributeTargets.Property |
         AttributeTargets.ReturnValue,
         Inherited = false)]
     public partial class DynamicallyAccessedMembersAttribute : Attribute
     {
     }
 }

Usage Examples

For a concrete example, see the existing code in EventSources which will create a manifest for a derived EventSource,

                         m_rawManifest = CreateManifestAndDescriptors(this.GetType(), Name, this);

If the EventSource Type is annotated with DynamicallyAccessedMembersAttribute as shown below, then the trimmer can preserve all methods of the derived EventSource and create a correct manifest for the derived EventSource type.

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
public class EventSource

Without such support, some event methods might not be included in the manifest generation, which can cause tool problems among other issue. See this customer comment for more details.

Alternative Designs

The API proposal #49448 on its own

Risks

This API might cause more code to be preserved than intended since it applies to all derived types of the annotated type.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions