【发布时间】:2016-09-27 11:49:01
【问题描述】:
我有以下函数,我想在其中遍历函数参数...
private static void TestExpression(Expression expr)
{
MethodCallExpression methodCall = expr as MethodCallExpression;
if(methodCall == null)
throw new ArgumentException("not a function call");
ReadOnlyCollection<Expression> args = methodCall.Arguments;
ParameterInfo[] param = methodCall.Method.GetParameters();
param.ToList().ForEach(p => Console.WriteLine(p.Name));
}
只是为了举例,我还有一个虚拟函数......
static int SomeFunc(int a, int b)
{
return 0;
}
问题是我不知道怎么把函数调用转换成Expression,也就是不知道怎么进行下面的调用
TestExpression(/*Something here converting SomeFunc(20, 30) to a Expression*/)
我想提一下,我对其他遍历参数的方法不感兴趣。我用 lambda 表达式尝试了多种变体,但没有成功......
【问题讨论】:
-
我忘了说使用的是.Net 3.5
标签: c# .net-3.5 expression