How do I call a generic method using a Type variable?
Asked Answered
E

9

1295

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<T>() 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<T>()?
        GenericMethod<myType>(); // This doesn't work

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

    public void GenericMethod<T>()
    {
        // ...
    }

    public static void StaticMethod<T>()
    {
        //...
    }
}
Exemplificative answered 24/10, 2008 at 5:17 Comment(4)
I tried Jon's solution and could not get it to work until I made the generic method public in my class. I know that another Jon replied saying that you need to specify the bindingflags but this did not help.Hunger
You also need BindingFlags.Instance, not just BindingFlags.NonPublic, to get the private/internal method.Martinmartina
Modern version of this question: https://mcmap.net/q/46491/-how-can-i-use-an-expression-tree-to-call-a-generic-method-when-the-type-is-only-known-at-runtime/103167Floaty
@Peter Mortensen - fyi I used spaces before the '?' to separate the English parts from the non-English (C#) parts; IMHO removing the space makes it look like the ? is part of the code. If there was no code, I'd certainly agree with removing the spaces, but in this case ...Exemplificative
N
1356

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.

Notochord answered 24/10, 2008 at 6:13 Comment(19)
+1; note that GetMethod() only considers public instance methods by default, so you may need BindingFlags.Static and/or BindingFlags.NonPublic.Crossing
The correct combination of flags is BindingFlags.NonPublic | BindingFlags.Instance (and optionally BindingFlags.Static).Martinmartina
@LarsKemmann: Why NonPublic when the desired method is public?Notochord
Sorry, I should have specified; that was in reply to @Jon of All Trades's comment on how to get at non-public methods. Your answer is correct without any binding flags for public methods, of course.Martinmartina
A question getting marked dupe of this wonders how to do this with static methods - and technically so does the question here. generic.Invoke()'s first parameter should be null when calling static methods. The first parameter is only necessary when calling instance methods.Psychosis
@ChrisMoschini: Added that to the answer.Notochord
This method is overkill for a lot of usecases, the use of dynamic can solve a lot of problem without using reflection. See @Mariusz answer (3rd).Daylong
@gzou: That's true now, yes. Look at the date of the question and answer though...Notochord
@JonSkeet Yes, just saying it for people arriving here from Google like me.Daylong
@gzou: I've added something to the answer - but note that for calling the generic methods in the question, dynamic doesn't help because type inference isn't available. (There are no arguments the compiler can use to determine the type argument.)Notochord
@JonSkeet, can you make that a function?Eichler
@Demodave: I'm not sure what you mean - just wrap that code in a method? Have you tried to do it yourself?Notochord
@JonSkeet This does not work if there are 2 methods with the same name, one generic and one non generic, an example is in the DbContext class of EntityFramework: public virtual DbSet<TEntity> Set<TEntity>() where TEntity : class; public virtual DbSet Set(Type entityType); Calling typeof(DbContext).GetMethod("Set") results in an AmbigiousMatchExceptionAllegiance
@SalmanHasratKhan: It will work when you've got the right method to start with - you just need to call GetMethods and look for the right method based on what you know about it. The call to MakeGenericMethod will then be the same.Notochord
@JonSkeet that is correct. I had to use GetMethods with a query to ensure that the method's IsGeneric property is true to select the right method.Allegiance
@SalmanHasratKhan: Right. You'd need to do something similar if you had a method overloaded in other ways, even if all of the methods were generic.Notochord
Also a good practice will be to get the method like this: MethodInfo method = typeof(Sample).GetMethods().Single(method => method.Name == nameof(Sample.GenericMethod));Bale
In my use case, I encountered an error: System.Reflection.TargetException Object does not match target type.. To resolve this exception, I had to replace this in the answer above with the variable that held the instance of the object that contained the method that I wanted to invoke. In the answer above, the variable I had to use was the equivalent of var sample = new Sample();, so I had to use generic.Invoke(sample, null);Underage
@derekmx271: Yes - I was responding to the question, in which the OP is trying to use reflection to call a method on this.Notochord
A
195

Just an addition to the original answer. While this will work:

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

It is also a little dangerous in that you lose compile-time check for GenericMethod. If you later do a refactoring and rename GenericMethod, this code won't notice and will fail at run time. Also, if there is any post-processing of the assembly (for example obfuscating or removing unused methods/classes) this code might break too.

So, if you know the method you are linking to at compile time, and this isn't called millions of times so overhead doesn't matter, I would change this code to be:

Action<> GenMethod = GenericMethod<int>;  //change int by any base type 
                                          //accepted by GenericMethod
MethodInfo method = this.GetType().GetMethod(GenMethod.Method.Name);
MethodInfo generic = method.MakeGenericMethod(myType);
generic.Invoke(this, null);

While not very pretty, you have a compile time reference to GenericMethod here, and if you refactor, delete or do anything with GenericMethod, this code will keep working, or at least break at compile time (if for example you remove GenericMethod).

Other way to do the same would be to create a new wrapper class, and create it through Activator. I don't know if there is a better way.

Appleton answered 27/2, 2011 at 16:11 Comment(8)
In cases where reflection is used to call a method, it's usual that the method name is itself discovered by another method. Knowing the method name in advance isn't common.Exemplificative
Well, I agree for common uses of reflection. But the original question was how to call "GenericMethod<myType>()" If that syntax was allowed, we wouldn't need GetMethod() at all. But for the question "how do I write "GenericMethod<myType>"? I think the answer should include a way to avoid losing the compile-time link with GenericMethod. Now if this question is common or not I don't know, but I do know I had this exact problem yesterday, and that's why I landed in this question.Appleton
You could do GenMethod.Method.GetGenericMethodDefinition() instead of this.GetType().GetMethod(GenMethod.Method.Name). It’s slightly cleaner and probably safer.Vernalize
What does mean "myType" in your sample?Cincinnatus
"myType" is a variable that holds a Type. If we knew the type, we could do GenericMethod<Type>(), but since we have the type in a variable, we use reflection.Constipate
Now you can use nameof(GenericMethod)Burgeon
When I try the first line Action<> GenMethod = GenericMethod<int>; I get a compiler error CS7003 Unexpected use of an unbound generic name for the left side and CS0123 No overload for 'GenericMethod' matches delegate 'Action<T>' for the right. I'm on .Net 4.5.Derzon
@EricScherrer: It should be Action not Action<>Floaty
R
182

Calling a generic method with a type parameter known only at runtime can be greatly simplified by using a dynamic type instead of the reflection API.

To use this technique the type must be known from the actual object (not just an instance of the Type class). Otherwise, you have to create an object of that type or use the standard reflection API solution. You can create an object by using the Activator.CreateInstance method.

If you want to call a generic method, that in "normal" usage would have had its type inferred, then it simply comes to casting the object of unknown type to dynamic. Here's an example:

class Alpha { }
class Beta { }
class Service
{
    public void Process<T>(T item)
    {
        Console.WriteLine("item.GetType(): " + item.GetType()
                          + "\ttypeof(T): " + typeof(T));
    }
}

class Program
{
    static void Main(string[] args)
    {
        var a = new Alpha();
        var b = new Beta();

        var service = new Service();
        service.Process(a); // Same as "service.Process<Alpha>(a)"
        service.Process(b); // Same as "service.Process<Beta>(b)"

        var objects = new object[] { a, b };
        foreach (var o in objects)
        {
            service.Process(o); // Same as "service.Process<object>(o)"
        }
        foreach (var o in objects)
        {
            dynamic dynObj = o;
            service.Process(dynObj); // Or write "service.Process((dynamic)o)"
        }
    }
}

And here's the output of this program:

item.GetType(): Alpha    typeof(T): Alpha
item.GetType(): Beta     typeof(T): Beta
item.GetType(): Alpha    typeof(T): System.Object
item.GetType(): Beta     typeof(T): System.Object
item.GetType(): Alpha    typeof(T): Alpha
item.GetType(): Beta     typeof(T): Beta

Process is a generic instance method that writes the real type of the passed argument (by using the GetType() method) and the type of the generic parameter (by using typeof operator).

By casting the object argument to dynamic type we deferred providing the type parameter until runtime. When the Process method is called with the dynamic argument then the compiler doesn't care about the type of this argument. The compiler generates code that at runtime checks the real types of passed arguments (by using reflection) and choose the best method to call. Here there is only this one generic method, so it's invoked with a proper type parameter.

In this example, the output is the same as if you wrote:

foreach (var o in objects)
{
    MethodInfo method = typeof(Service).GetMethod("Process");
    MethodInfo generic = method.MakeGenericMethod(o.GetType());
    generic.Invoke(service, new object[] { o });
}

The version with a dynamic type is definitely shorter and easier to write. You also shouldn't worry about performance of calling this function multiple times. The next call with arguments of the same type should be faster thanks to the caching mechanism in DLR. Of course, you can write code that cache invoked delegates, but by using the dynamic type you get this behaviour for free.

If the generic method you want to call don't have an argument of a parametrized type (so its type parameter can't be inferred) then you can wrap the invocation of the generic method in a helper method like in the following example:

class Program
{
    static void Main(string[] args)
    {
        object obj = new Alpha();

        Helper((dynamic)obj);
    }

    public static void Helper<T>(T obj)
    {
        GenericMethod<T>();
    }

    public static void GenericMethod<T>()
    {
        Console.WriteLine("GenericMethod<" + typeof(T) + ">");
    }
}

Increased type safety

What is really great about using dynamic object as a replacement for using reflection API is that you only lose compile time checking of this particular type that you don't know until runtime. Other arguments and the name of the method are staticly analysed by the compiler as usual. If you remove or add more arguments, change their types or rename method name then you'll get a compile-time error. This won't happen if you provide the method name as a string in Type.GetMethod and arguments as the objects array in MethodInfo.Invoke.

Below is a simple example that illustrates how some errors can be caught at compile time (commented code) and other at runtime. It also shows how the DLR tries to resolve which method to call.

interface IItem { }
class FooItem : IItem { }
class BarItem : IItem { }
class Alpha { }

class Program
{
    static void Main(string[] args)
    {
        var objects = new object[] { new FooItem(), new BarItem(), new Alpha() };
        for (int i = 0; i < objects.Length; i++)
        {
            ProcessItem((dynamic)objects[i], "test" + i, i);

            //ProcesItm((dynamic)objects[i], "test" + i, i);
            //compiler error: The name 'ProcesItm' does not
            //exist in the current context

            //ProcessItem((dynamic)objects[i], "test" + i);
            //error: No overload for method 'ProcessItem' takes 2 arguments
        }
    }

    static string ProcessItem<T>(T item, string text, int number)
        where T : IItem
    {
        Console.WriteLine("Generic ProcessItem<{0}>, text {1}, number:{2}",
                          typeof(T), text, number);
        return "OK";
    }
    static void ProcessItem(BarItem item, string text, int number)
    {
        Console.WriteLine("ProcessItem with Bar, " + text + ", " + number);
    }
}

Here we again execute some method by casting the argument to the dynamic type. Only verification of first argument's type is postponed to runtime. You will get a compiler error if the name of the method you're calling doesn't exist or if other arguments are invalid (wrong number of arguments or wrong types).

When you pass the dynamic argument to a method then this call is lately bound. Method overload resolution happens at runtime and tries to choose the best overload. So if you invoke the ProcessItem method with an object of BarItem type then you'll actually call the non-generic method, because it is a better match for this type. However, you'll get a runtime error when you pass an argument of the Alpha type because there's no method that can handle this object (a generic method has the constraint where T : IItem and Alpha class doesn't implement this interface). But that's the whole point. The compiler doesn't have information that this call is valid. You as a programmer know this, and you should make sure that this code runs without errors.

Return type gotcha

When you're calling a non-void method with a parameter of dynamic type, its return type will probably be dynamic too. So if you'd change previous example to this code:

var result = ProcessItem((dynamic)testObjects[i], "test" + i, i);

then the type of the result object would be dynamic. This is because the compiler don't always know which method will be called. If you know the return type of the function call then you should implicitly convert it to the required type so the rest of the code is statically typed:

string result = ProcessItem((dynamic)testObjects[i], "test" + i, i);

You'll get a runtime error if the type doesn't match.

Actually, if you try to get the result value in the previous example then you'll get a runtime error in the second loop iteration. This is because you tried to save the return value of a void function.

Radio answered 16/3, 2014 at 19:21 Comment(3)
Mariusz, confused by "However, you'll get runtime error when you pass argument of Alpha type because there's no method that can handle this object. " If I call var a = new Alpha() ProcessItem(a,"test" + i, i) Why wouldn't the generic ProcessItem method handle this effectively, outputting "General Process Item"?Dendro
@AlexEdelstein I edited my answer to clarify a bit. It's because generic ProcessItem method has generic constraint and accepts only object that implements IItem interface. When you will call ProcessItem(new Aplha(), "test" , 1); or ProcessItem((object)(new Aplha()), "test" , 1); you'll get a compiler error but when casting to dynamic you postpone that check to runtime.Radio
Great answer and explanation, works perfectly for me. Much better than the accepted answer, shorter to write, more performant, and safer.Ratsbane
T
24

Adding on to Adrian Gallero's answer:

Calling a generic method from type info involves three steps.

##TLDR: Calling a known generic method with a type object can be accomplished by:##

((Action)GenericMethod<object>)
    .Method
    .GetGenericMethodDefinition()
    .MakeGenericMethod(typeof(string))
    .Invoke(this, null);

where GenericMethod<object> is the method name to call and any type that satisfies the generic constraints.

(Action) matches the signature of the method to be called i.e. (Func<string,string,int> or Action<bool>)

##Step 1 is getting the MethodInfo for the generic method definition##

###Method 1: Use GetMethod() or GetMethods() with appropriate types or binding flags.###

MethodInfo method = typeof(Sample).GetMethod("GenericMethod");

###Method 2: Create a delegate, get the MethodInfo object and then call GetGenericMethodDefinition

From inside the class that contains the methods:

MethodInfo method = ((Action)GenericMethod<object>)
    .Method
    .GetGenericMethodDefinition();

MethodInfo method = ((Action)StaticMethod<object>)
    .Method
    .GetGenericMethodDefinition();

From outside of the class that contains the methods:

MethodInfo method = ((Action)(new Sample())
    .GenericMethod<object>)
    .Method
    .GetGenericMethodDefinition();

MethodInfo method = ((Action)Sample.StaticMethod<object>)
    .Method
    .GetGenericMethodDefinition();

In C#, the name of a method, i.e. "ToString" or "GenericMethod" actually refers to a group of methods that may contain one or more methods. Until you provide the types of the method parameters, it is not known which method you are referring to.

((Action)GenericMethod<object>) refers to the delegate for a specific method. ((Func<string, int>)GenericMethod<object>) refers to a different overload of GenericMethod

###Method 3: Create a lambda expression containing a method call expression, get the MethodInfo object and then GetGenericMethodDefinition

MethodInfo method = ((MethodCallExpression)((Expression<Action<Sample>>)(
    (Sample v) => v.GenericMethod<object>()
    )).Body).Method.GetGenericMethodDefinition();

This breaks down to

Create a lambda expression where the body is a call to your desired method.

Expression<Action<Sample>> expr = (Sample v) => v.GenericMethod<object>();

Extract the body and cast to MethodCallExpression

MethodCallExpression methodCallExpr = (MethodCallExpression)expr.Body;

Get the generic method definition from the method

MethodInfo methodA = methodCallExpr.Method.GetGenericMethodDefinition();

##Step 2 is calling MakeGenericMethod to create a generic method with the appropriate type(s).##

MethodInfo generic = method.MakeGenericMethod(myType);

##Step 3 is invoking the method with the appropriate arguments.##

generic.Invoke(this, null);
Turgor answered 9/1, 2015 at 22:20 Comment(0)
K
19

With C# 4.0, reflection isn't necessary as the DLR can call it using runtime types. Since using the DLR library is kind of a pain dynamically (instead of the C# compiler generating code for you), the open source framework Dynamitey (.net standard 1.5) gives you easy cached run-time access to the same calls the compiler would generate for you.

var name = InvokeMemberName.Create;
Dynamic.InvokeMemberAction(this, name("GenericMethod", new[]{myType}));


var staticContext = InvokeContext.CreateStatic;
Dynamic.InvokeMemberAction(staticContext(typeof(Sample)), name("StaticMethod", new[]{myType}));
Kotto answered 5/7, 2011 at 13:40 Comment(0)
C
13

Nobody provided the "classic Reflection" solution, so here is a complete code example:

using System;
using System.Collections;
using System.Collections.Generic;

namespace DictionaryRuntime
{
    public class DynamicDictionaryFactory
    {
        /// <summary>
        /// Factory to create dynamically a generic Dictionary.
        /// </summary>
        public IDictionary CreateDynamicGenericInstance(Type keyType, Type valueType)
        {
            //Creating the Dictionary.
            Type typeDict = typeof(Dictionary<,>);

            //Creating KeyValue Type for Dictionary.
            Type[] typeArgs = { keyType, valueType };

            //Passing the Type and create Dictionary Type.
            Type genericType = typeDict.MakeGenericType(typeArgs);

            //Creating Instance for Dictionary<K,T>.
            IDictionary d = Activator.CreateInstance(genericType) as IDictionary;

            return d;

        }
    }
}

The above DynamicDictionaryFactory class has a method

CreateDynamicGenericInstance(Type keyType, Type valueType)

and it creates and returns an IDictionary instance, the types of whose keys and values are exactly the specified on the call keyType and valueType.

Here is a complete example how to call this method to instantiate and use a Dictionary<String, int> :

using System;
using System.Collections.Generic;

namespace DynamicDictionary
{
    class Test
    {
        static void Main(string[] args)
        {
            var factory = new DictionaryRuntime.DynamicDictionaryFactory();
            var dict = factory.CreateDynamicGenericInstance(typeof(String), typeof(int));

            var typedDict = dict as Dictionary<String, int>;

            if (typedDict != null)
            {
                Console.WriteLine("Dictionary<String, int>");

                typedDict.Add("One", 1);
                typedDict.Add("Two", 2);
                typedDict.Add("Three", 3);

                foreach(var kvp in typedDict)
                {
                    Console.WriteLine("\"" + kvp.Key + "\": " + kvp.Value);
                }
            }
            else
                Console.WriteLine("null");
        }
    }
}

When the above console application is executed, we get the correct, expected result:

Dictionary<String, int>
"One": 1
"Two": 2
"Three": 3
Correct answered 24/8, 2016 at 1:24 Comment(0)
R
5

This is my 2 cents based on Grax's answer, but with two parameters required for a generic method.

Assume your method is defined as follows in an Helpers class:

public class Helpers
{
    public static U ConvertCsvDataToCollection<U, T>(string csvData)
    where U : ObservableCollection<T>
    {
      //transform code here
    }
}

In my case, U type is always an observable collection storing object of type T.

As I have my types predefined, I first create the "dummy" objects that represent the observable collection (U) and the object stored in it (T) and that will be used below to get their type when calling the Make

object myCollection = Activator.CreateInstance(collectionType);
object myoObject = Activator.CreateInstance(objectType);

Then call the GetMethod to find your Generic function:

MethodInfo method = typeof(Helpers).
GetMethod("ConvertCsvDataToCollection");

So far, the above call is pretty much identical as to what was explained above but with a small difference when you need have to pass multiple parameters to it.

You need to pass an Type[] array to the MakeGenericMethod function that contains the "dummy" objects' types that were create above:

MethodInfo generic = method.MakeGenericMethod(
new Type[] {
   myCollection.GetType(),
   myObject.GetType()
});

Once that's done, you need to call the Invoke method as mentioned above.

generic.Invoke(null, new object[] { csvData });

And you're done. Works a charm!

UPDATE:

As @Bevan highlighted, I do not need to create an array when calling the MakeGenericMethod function as it takes in params and I do not need to create an object in order to get the types as I can just pass the types directly to this function. In my case, since I have the types predefined in another class, I simply changed my code to:

object myCollection = null;

MethodInfo method = typeof(Helpers).
GetMethod("ConvertCsvDataToCollection");

MethodInfo generic = method.MakeGenericMethod(
   myClassInfo.CollectionType,
   myClassInfo.ObjectType
);

myCollection = generic.Invoke(null, new object[] { csvData });

myClassInfo contains 2 properties of type Type which I set at run time based on an enum value passed to the constructor and will provide me with the relevant types which I then use in the MakeGenericMethod.

Thanks again for highlighting this @Bevan.

Rembrandt answered 22/10, 2015 at 23:28 Comment(1)
The arguments to MakeGenericMethod() have the params keyword so you don't need to create an array; nor do you need to create instances to get the types - methodInfo.MakeGenericMethod(typeof(TCollection), typeof(TObject)) would be sufficient.Exemplificative
S
1

Inspired by Enigmativity's answer - let's assume you have two (or more) classes, like

public class Bar { }
public class Square { }

and you want to call the method Foo<T> with Bar and Square, which is declared as

public class myClass
{
    public void Foo<T>(T item)
    {
        Console.WriteLine(typeof(T).Name);
    }
}

Then you can implement an Extension method like:

public static class Extension
{
    public static void InvokeFoo<T>(this T t)
    {
        var fooMethod = typeof(myClass).GetMethod("Foo");
        var tType = typeof(T);
        var fooTMethod = fooMethod.MakeGenericMethod(new[] { tType });
        fooTMethod.Invoke(new myClass(), new object[] { t });
    }
}

With this, you can simply invoke Foo like:

var objSquare = new Square();
objSquare.InvokeFoo();

var objBar = new Bar();
objBar.InvokeFoo();

which works for every class. In this case, it will output:

Square
Bar

Semicolon answered 3/3, 2020 at 10:8 Comment(0)
H
1

In spite this is a quite old question, I found it interesting, because there are several options to call a method dynamically. Literally, it is reflection, expression trees, and emitter. Historically, reflection is the slowest option, whilst emitter is the fastest one. Therefore, I've decided to compare them in this intriguing case and figure out if there are any changes nowadays. The original question asks for ** the best way to call a generic method when the type parameter isn't known at compile time**. However, almost all answers above suggested to use reflection.

I have created three test cases for all mentioned approaches. First, here is a slightly modified sample class that is going to be tested with 3 methods: TestReflection, TestExpression, and TestEmit.

public class Sample
{
    public void TestDirectCall(Type type)
    {
        GenericMethod<string>();
        GenericMethodWithArg<string>(42);
        StaticMethod<string>();
        StaticMethodWithArg<string>(6);
    }

    public void TestReflection(Type type)
    {
        CallViaReflection.CallGenericMethod(this, type);
        CallViaReflection.CallGenericMethod(this, type, 42);
        CallViaReflection.CallStaticMethod(type);
        CallViaReflection.CallStaticMethod(type, 6);
    }

    public void TestExpression(Type type)
    {
        CallViaExpression.CallGenericMethod(this, type);
        CallViaExpression.CallGenericMethod(this, type, 42);
        CallViaExpression.CallStaticMethod(type);
        CallViaExpression.CallStaticMethod(type, 6);
    }

    public void TestEmit(Type type)
    {
        CallViaEmit.CallGenericMethod(this, type);
        CallViaEmit.CallGenericMethod(this, type, 42);
        CallViaEmit.CallStaticMethod(type);
        CallViaEmit.CallStaticMethod(type, 6);
    }

    public void T()
    {
        StaticMethod<string>();
    }

    public void GenericMethod<T>()
    {
    }

    public void GenericMethodWithArg<T>(int someValue)
    {
    }

    public static void StaticMethod<T>()
    {
    }

    public static void StaticMethodWithArg<T>(int someValue)
    {
    }
}

The class CallViaReflection represents a helper class that makes calls of generic methods via reflection. I have decided to introduce a cache for better results.

public static class CallViaReflection
{
    private readonly static Cache<MethodInfo> cache = new();

    public static void CallGenericMethod(Sample sample, Type genericType)
    {
        var callDelegate = GetDelegate(nameof(Sample.GenericMethod), BindingFlags.Instance | BindingFlags.Public, genericType);
        callDelegate.Invoke(sample, null);
    }

    public static void CallGenericMethod(Sample sample, Type genericType, int someValue)
    {
        var callDelegate = GetDelegate(nameof(Sample.GenericMethodWithArg), BindingFlags.Instance | BindingFlags.Public, genericType, typeof(int));
        callDelegate.Invoke(sample, new object[] { someValue });
    }

    public static void CallStaticMethod(Type genericType)
    {
        var callDelegate = GetDelegate(nameof(Sample.StaticMethod), BindingFlags.Static | BindingFlags.Public, genericType);
        callDelegate.Invoke(null, null);
    }

    public static void CallStaticMethod(Type genericType, int someValue)
    {
        var callDelegate = GetDelegate(nameof(Sample.StaticMethodWithArg), BindingFlags.Static | BindingFlags.Public, genericType, typeof(int));
        callDelegate.Invoke(null, new object[] { someValue });
    }

    private static MethodInfo GetDelegate(string methodName, BindingFlags bindingFlags, Type genericType, params Type[] arguments)
    {
        if (cache.TryGet(methodName, genericType, out var concreteMethodInfo))
            return concreteMethodInfo;

        var sampleType = typeof(Sample);
        MethodInfo genericMethodInfo = sampleType.GetMethod(methodName, bindingFlags)!;
        concreteMethodInfo = genericMethodInfo.MakeGenericMethod(genericType);
        cache.Add(methodName, genericType, concreteMethodInfo);
        return concreteMethodInfo;
    }
}

The next class CallViaExpression uses cached expression trees.

public static class CallViaExpression
{
    private static readonly Cache<Delegate> cache = new();

    public static void CallGenericMethod(Sample sample, Type genericType)
    {
        var callDelegate = GetDelegate(nameof(Sample.GenericMethod), BindingFlags.Instance | BindingFlags.Public, genericType);
        ((Action<Sample>)callDelegate).Invoke(sample);
    }

    public static void CallGenericMethod(Sample sample, Type genericType, int someValue)
    {
        var callDelegate = GetDelegate(nameof(Sample.GenericMethodWithArg), BindingFlags.Instance | BindingFlags.Public, genericType, typeof(int));
        ((Action<Sample, int>)callDelegate).Invoke(sample, someValue);
    }

    public static void CallStaticMethod(Type genericType)
    {
        var callDelegate = GetDelegate(nameof(Sample.StaticMethod), BindingFlags.Static | BindingFlags.Public, genericType);
        ((Action)callDelegate).Invoke();
    }

    public static void CallStaticMethod(Type genericType, int someValue)
    {
        var callDelegate = GetDelegate(nameof(Sample.StaticMethodWithArg), BindingFlags.Static | BindingFlags.Public, genericType, typeof(int));
        ((Action<int>)callDelegate).Invoke(someValue);
    }

    private static Delegate GetDelegate(string methodName, BindingFlags bindingFlags, Type genericType, params Type[] arguments)
    {
        if (cache.TryGet(methodName, genericType, out var callDelegate))
            return callDelegate;

        var sampleType = typeof(Sample);
        MethodInfo genericMethodInfo = sampleType.GetMethod(methodName, bindingFlags)!;
        var concreteMethodInfo = genericMethodInfo.MakeGenericMethod(genericType);

        var argumentExpr = arguments.Select((type, i) => Expression.Parameter(type, "arg" + i)).ToArray();
        if (concreteMethodInfo.IsStatic)
        {
            var callExpr = Expression.Call(concreteMethodInfo, argumentExpr);
            callDelegate = Expression.Lambda(callExpr, argumentExpr).Compile();
        }
        else
        {
            var parameterExpr = Expression.Parameter(sampleType, "sample");
            var callExpr = Expression.Call(parameterExpr, concreteMethodInfo, argumentExpr);
            callDelegate = Expression.Lambda(callExpr, new[] { parameterExpr }.Union(argumentExpr).ToArray()).Compile();
        }

        cache.Add(methodName, genericType, callDelegate);
        return callDelegate;
    }
}

And the last but not least CallViaEmit emits necessary operations.

public static class CallViaEmit
{
    private static readonly Cache<Delegate> cache = new();

    public static void CallGenericMethod(this Sample sample, Type genericType)
    {
        var callDelegate = GetDynamicMethod(nameof(Sample.GenericMethod), BindingFlags.Instance | BindingFlags.Public, genericType);
        ((Action<Sample>)callDelegate).Invoke(sample);
    }

    public static void CallGenericMethod(this Sample sample, Type genericType, int someValue)
    {
        var callDelegate = GetDynamicMethod(nameof(Sample.GenericMethodWithArg), BindingFlags.Instance | BindingFlags.Public, genericType);
        ((Action<Sample, int>)callDelegate).Invoke(sample, someValue);
    }

    public static void CallStaticMethod(Type genericType)
    {
        var callDelegate = GetDynamicMethod(nameof(Sample.StaticMethod), BindingFlags.Static | BindingFlags.Public, genericType);
        ((Action)callDelegate).Invoke();
    }

    public static void CallStaticMethod(Type genericType, int someValue)
    {
        var callDelegate = GetDynamicMethod(nameof(Sample.StaticMethodWithArg), BindingFlags.Static | BindingFlags.Public, genericType);
        ((Action<int>)callDelegate).Invoke(someValue);
    }

    private static Delegate GetDynamicMethod(string methodName, BindingFlags bindingFlags, Type genericType)
    {
        if (cache.TryGet(methodName, genericType, out var callDelegate))
            return callDelegate;

        var genericMethodInfo = typeof(Sample).GetMethod(methodName, bindingFlags)!;
        var concreteMethodInfo = genericMethodInfo.MakeGenericMethod(genericType);
        var argumentTypes = concreteMethodInfo.GetParameters().Select(x => x.ParameterType).ToArray(); ;
        var dynamicMethodArgs = concreteMethodInfo.IsStatic
            ? argumentTypes
            : new[] { typeof(Sample) }.Union(argumentTypes).ToArray();

        var dynamicMethod = new DynamicMethod("DynamicCall", null, dynamicMethodArgs);
        var il = dynamicMethod.GetILGenerator();
        il.Emit(OpCodes.Nop);

        switch (dynamicMethodArgs.Length)
        {
            case 0:
                break;
            case 1:
                il.Emit(OpCodes.Ldarg_0);
                break;
            case 2:
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldarg_1);
                break;
            case 3:
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldarg_1);
                il.Emit(OpCodes.Ldarg_2);
                break;
            default:
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldarg_1);
                il.Emit(OpCodes.Ldarg_2);
                il.Emit(OpCodes.Ldarg_3);
                for (int i = 4; i < argumentTypes.Length; i++)
                {
                    il.Emit(OpCodes.Ldarg, argumentTypes[i]);
                }
                break;
        }

        il.EmitCall(concreteMethodInfo.IsStatic ? OpCodes.Call : OpCodes.Callvirt, concreteMethodInfo, null);
        il.Emit(OpCodes.Nop);
        il.Emit(OpCodes.Ret);

        callDelegate = dynamicMethod.CreateDelegate(GetActionType(dynamicMethodArgs));
        cache.Add(methodName, genericType, callDelegate);
        return callDelegate;
    }

    private static Type GetActionType(Type[] argumentTypes)
    {
        switch (argumentTypes.Length)
        {
            case 0:
                return typeof(Action);
            case 1:
                return typeof(Action<>).MakeGenericType(argumentTypes);
            case 2:
                return typeof(Action<,>).MakeGenericType(argumentTypes);
            case 3:
                return typeof(Action<,,>).MakeGenericType(argumentTypes);
            case 4:
                return typeof(Action<,,,>).MakeGenericType(argumentTypes);
            case 5:
                return typeof(Action<,,,,>).MakeGenericType(argumentTypes);
            case 6:
                return typeof(Action<,,,,,>).MakeGenericType(argumentTypes);
            case 7:
                return typeof(Action<,,,,,,>).MakeGenericType(argumentTypes);
            case 8:
                return typeof(Action<,,,,,,,>).MakeGenericType(argumentTypes);
            default:
                throw new NotSupportedException("Action with more than 8 arguments is not supported");
        }
    }
}

Finally, here is a test class and test results.

[TestFixture]
public class SampleTests
{
    private const int Iterations = 10000000;

    [Test]
    public void TestDirectCall()
    {
        var sample = new Sample();
        var stopwatch = new Stopwatch();
        stopwatch.Start();

        for (var i = 0; i < Iterations; i++)
            sample.TestDirectCall(typeof(string));

        stopwatch.Stop();
        Assert.Pass($"Calling methods directly took {stopwatch.ElapsedMilliseconds} milliseconds.");
    }

    [Test]
    public void TestReflection()
    {
        var sample = new Sample();
        var stopwatch = new Stopwatch();
        stopwatch.Start();

        for (var i = 0; i < Iterations; i++)
            sample.TestReflection(typeof(string));

        stopwatch.Stop();
        Assert.Pass($"Calling methods dynamically via reflection took {stopwatch.ElapsedMilliseconds} milliseconds.");
    }

    [Test]
    public void TestExpressionTree()
    {
        var sample = new Sample();
        var stopwatch = new Stopwatch();
        stopwatch.Start();

        for (var i = 0; i < Iterations; i++)
            sample.TestExpression(typeof(string));

        stopwatch.Stop();
        Assert.Pass($"Calling methods dynamically via expression tree took {stopwatch.ElapsedMilliseconds} milliseconds.");
    }

    [Test]
    public void TestEmit()
    {
        var sample = new Sample();
        var stopwatch = new Stopwatch();
        stopwatch.Start();

        for (var i = 0; i < Iterations; i++)
            sample.TestEmit(typeof(string));

        stopwatch.Stop();
        Assert.Pass($"Calling methods dynamically via emit took {stopwatch.ElapsedMilliseconds} milliseconds.");
    }
}

Calling methods dynamically via emit took 2939 milliseconds. Calling methods dynamically via expression tree took 3910 milliseconds. Calling methods dynamically via reflection took 6381 milliseconds.

Obviously, the winner is emitter. It still performs more than twice faster. Second place is for expression trees.

Therefore, my verdict has not beeing changed for second decade yet. If you need to call a method dynamically, start going with expression trees. If your code is performance-critical, use ILGenerator and emit necessary calls. In spite, it might look complicated at first glance, all necessary MSIL instruction could be easily disassembled using ildasm.

The source code is available on GitHub.

Hanover answered 24/7, 2023 at 15:54 Comment(2)
If your code is not performance-critical then I suggest you use the most readable option, that is reflection.Brasil
@LucaCremonesi it's about habits. When I got used to use expression trees I stopped using reflection. Expression trees are quite easy when you understand them, but honestly learning curve is hardHanover

© 2022 - 2024 — McMap. All rights reserved.