我有以下通用方法
public static T2 MapCollection<T1, T2, T3, T4>(T1 source)
where T1 : ICollection<T3>
where T2 : ICollection<T4>
{
...
}
这里的目标是使用反射调用此方法,因此我需要MakeGenericMethod ...
我是这样做的:
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);
然后,我尝试调用方法..
object[] parametersArray = new object[] { value }; // value is set earlier in the code (PropertyInfo.GetValue(...))
object foo = mi.Invoke(null, parametersArray);
我得到以下异常:
System.InvalidOperationException: Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.
我认为这与Generic Method有4个泛型参数且只有一个参数这一事实有关......
我对此缺少什么?