Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetTypeInfo().GetDeclaredMethod() generates a false positive warning #2483

Closed
roji opened this issue Jan 6, 2022 · 6 comments
Closed

GetTypeInfo().GetDeclaredMethod() generates a false positive warning #2483

roji opened this issue Jan 6, 2022 · 6 comments

Comments

@roji
Copy link
Member

roji commented Jan 6, 2022

It seems that while the linker properly recognizes Type.GetMethod() and Type.GetTypeInfo().GetMethod(), it generates a warning when Type.GetTypeInfo().GetDeclaredMethod() is used (but the program does work):

class Program
{
    public static void Main()
    {
        // These works:
        // var openMethod = typeof(Program).GetMethod("Foo")!;
        // var openMethod = typeof(Program).GetTypeInfo().GetMethod(nameof(Foo))!;

        // This works, but generates a warning:
        var openMethod = typeof(Program).GetTypeInfo().GetDeclaredMethod(nameof(Foo))!;

        var method = openMethod.MakeGenericMethod(typeof(string));
        method.Invoke(null, null);
    }

    public static void Foo<T>()
        => Console.WriteLine("Foo" + default(T));
}

/cc @vitek-karas

@vitek-karas
Copy link
Member

What is the warning it generates?

The GetDeclaredMethod is annotated like this:

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
public virtual MethodInfo? GetDeclaredMethod(string name) ...

Meaning that the "this" passed to it must keep all methods (public and private).

Unlike Type.GetMethod trimmer doesn't have a special case for this, so it doesn't matter if the name of the method is statically known or not, the trimmer will simply keep all methods on the target type.

@roji
Copy link
Member Author

roji commented Jan 6, 2022

Sorry for leaving out the details. The problem isn't with GetDeclaredMethod, it's with MakeGenericMethod called on its result:

/home/roji/projects/test/Program.cs(20,9): Trim analysis warning IL2060: Program.Main(): Call to 'System.Reflection.MethodInfo System.Reflection.MethodInfo::MakeGenericMethod(System.Type[])' can not be statically analyzed. It's not possible to guarantee the availability of requirements of the generic method. [/home/roji/projects/test/Test.csproj]

@vitek-karas
Copy link
Member

vitek-karas commented Jan 6, 2022

I see - that's the same root cause as I mentioned above that there's no intrinsic handling for TypeInfo.GetDeclaredMethod - meaning linker doesn't know which method the MakeGenericMethod is called on.

We could add that if necessary... it's just that for now we didn't find the need.

(Given that TypeInfo is basically obsolete now)

@roji
Copy link
Member Author

roji commented Jan 6, 2022

We have quite a lot of usage of TypeInfo in the code base - I can't say it's super important or anything, mainly that the APIs there are sometimes slightly easier to use. If TypeInfo really is discouraged by now, we can make the switch where necessary...

@MichalStrehovsky
Copy link
Member

Yeah, it's pretty discouraged: dotnet/runtime#61122

@roji
Copy link
Member Author

roji commented Jan 6, 2022

Thanks @MichalStrehovsky, I'll go ahead and close this, and remove any problematic references to TypeInfo.

@roji roji closed this as completed Jan 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants