vendredi 24 juillet 2015

c# Reflection MakeGenericMethod multiple generic types in method definition [duplicate]

This question already has an answer here:

I have the following generic method

 public static T2 MapCollection<T1, T2, T3, T4>(T1 source) 
            where T1 : ICollection<T3> 
            where T2 : ICollection<T4>
        {
          ...
        }

The goal here is to call this method using reflection so I need to MakeGenericMethod...

I do this like this :

Type type1 = prop1.PropertyType;
Type type2 = prop2.PropertyType;
Type type3 = prop3.PropertyType;
Type type4 = prop4.PropertyType;

MethodInfo mi = typeof (DynamicMapper).GetMethod("MapCollection");

Type[] typeArgs = { type1, type2, type3, type4 };

mi.MakeGenericMethod(typeArgs);

Then, I try to invoke the method..

object[] parametersArray = new object[] { value }; // value is set earlier in the code (PropertyInfo.GetValue(...))
object foo = mi.Invoke(null, parametersArray);

I get the following exception:

System.InvalidOperationException: Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.

I think this has something to do with the fact the the Generic Method has 4 generics arguments and only one parameter...

What am my missing on this?





Aucun commentaire:

Enregistrer un commentaire