Skip to content

Reduce noisy warnings for compiler generated code marked as reflectable #2906

@vitek-karas

Description

@vitek-karas

Take this example:

// Some other library
class Provider
{
    public static void Register<[DynamicallyAccessedMembers.All] T>() { ... }
}

// The app
class MyFancyProvider
{
    public static void Register ()
    {
        var a = () => {
            Provider.Register<MyFancyProvider> ();  // IL2118
        };

        a();
    }
}

The IL2118 produces by this code is unnecessary. The warning is meant to cover cases where the lambda contains potentially dangerous code and calling it directly via reflection could lead into analysis holes. That's not the case here, the lambda uses no external state from the outer scope (no method parameters, no closure) and as such acts basically as a normal method.

The warning is expected to be somewhat noisy in some cases, but we hoped it would not be too common. The above pattern though can prove to be problematic - especially since it's very hard for the user to figure out what happened - and we're forcing the user to suppress the warning (which is always bad).

/cc @sbomer

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions