Skip to content

Enhance DynamicallyAccessedMembers Attribute to apply to class  #49465

Closed

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.

Activity

  1. added
    api-suggestion
    on Mar 11, 2021
  2. ghost added
    untriaged
    on Mar 11, 2021
  3. LakshanF commented on Mar 11, 2021

    @LakshanF
    ContributorAuthor
  4. added
    api-ready-for-review
    and removed
    api-suggestion
    untriaged
    on Mar 11, 2021
  5. added
    blocking
    on Mar 11, 2021
  6. added this to the 6.0.0 milestone on Mar 12, 2021
  7. added
    api-approved
    and removed
    api-ready-for-review
    on Mar 16, 2021
  8. terrajobst commented on Mar 16, 2021

    @terrajobst
    Contributor
    • Looks good as proposed
     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
         {
         }
     }
  9. removed
    blocking
    on Mar 16, 2021
  10. self-assigned this
    on Mar 22, 2021
  11. eerhardt commented on Apr 13, 2021

    @eerhardt
    Member

    Resolved by #49778.

  12. ghost locked as resolved and limited conversation to collaborators on May 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Diagnostics

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions