vendredi 26 juillet 2019

How to execute generic method by setting double generic using reflection

I have a class and it has generic method:

   public class MyClass
   {
      public void MyMethod<T, IEntity>()
      {

      }
   }

In some places I'm executing it using reflection:

        var intType = typeof(int);
        var stringType = typeof(string);
        MethodInfo method = typeof(MyClass).GetMethod(nameof(MyClass.MyMethod));
        MethodInfo generic = method.MakeGenericMethod(intType, stringType);
        generic.Invoke(myClass, null);

It works. In some cases I need to use Action in method:

myClass.MyMethod<Action<int>, string>();

How can I do that using reflection?





Aucun commentaire:

Enregistrer un commentaire