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

Skip to content

Trim analysis doesn't understand receiver type for calls to GetType() #100786

@sbomer

Description

@sbomer

DynamicallyAccessedMembers annotations on a type will treat the return value of GetType as satisfying the annotation, for a call to this.GetType(), but not for a call to c.GetType() where c is assignable to the annotated type.

This causes a warning on the call to Require(c.GetType()) in the sample below. It's possible to work around this by defining an annotated helper on the type that just returns this.GetType(), but this ideally wouldn't be necessary:

using System.Diagnostics.CodeAnalysis;

C c = new();
Require(c.GetType()); // Warning

Require(c.GetTypeWithConstructors()); // OK

static void Require([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] Type type) {}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
class C {
    [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
    public Type GetTypeWithConstructors() => GetType();
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions