我在这里遗漏了一些微不足道的东西。假设我有这样的方法:
abstract class C
{
public static void M(Type t, params int[] i)
{
}
}
我正在学习表达式树,我需要构建一个使用一些预定义参数调用此方法的委托。问题是我不知道选择正确的重载并传递Expression.Call
.
我想实现这一点:
//I have other overloads for M, hence I need to specify the type of arugments
var methodInfo = typeof(C).GetMethod("M", new Type[] { typeof(Type), typeof(int[]) });
//this is the first argument to method M, not sure if I have chosen the right expression
var typeArgumentExp = Expression.Parameter(someType);
var intArrayArgumentExp = Enumerable.Repeat(Expression.Constant(0), 3);
var combinedArgumentsExp = new Expression[] { typeArgumentExp }.Concat(intArrayArgumentExp);
var call = Expression.Call(methodInfo, combinedArgumentsExp);
在Expression.Call
我得到的那一行:
System.Core.dll 中出现“System.ArgumentException”类型的未处理异常
附加信息:为调用方法 'Void M(System.Type, Int32[])' 提供的参数数量不正确
我哪里出错了?