HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpCritical

How do I call a generic method using a Type variable?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howusingmethodvariablecallgenerictype

Problem

What's the best way to call a generic method when the type parameter isn't known at compile time, but instead is obtained dynamically at runtime?

Consider the following sample code - inside the Example() method, what's the most concise way to invoke GenericMethod() using the Type stored in the myType variable?

public class Sample
{
    public void Example(string typeName)
    {
        Type myType = FindType(typeName);

        // What goes here to call GenericMethod()?
        GenericMethod(); // This doesn't work

        // What changes to call StaticMethod()?
        Sample.StaticMethod(); // This also doesn't work
    }

    public void GenericMethod()
    {
        // ...
    }

    public static void StaticMethod()
    {
        //...
    }
}

Solution

You need to use reflection to get the method to start with, then "construct" it by supplying type arguments with MakeGenericMethod:

MethodInfo method = typeof(Sample).GetMethod(nameof(Sample.GenericMethod));
MethodInfo generic = method.MakeGenericMethod(myType);
generic.Invoke(this, null);


For a static method, pass null as the first argument to Invoke. That's nothing to do with generic methods - it's just normal reflection.

As noted, a lot of this is simpler as of C# 4 using dynamic - if you can use type inference, of course. It doesn't help in cases where type inference isn't available, such as the exact example in the question.

Code Snippets

MethodInfo method = typeof(Sample).GetMethod(nameof(Sample.GenericMethod));
MethodInfo generic = method.MakeGenericMethod(myType);
generic.Invoke(this, null);

Context

Stack Overflow Q#232535, score: 1375

Revisions (0)

No revisions yet.