【发布时间】:2014-03-28 07:52:22
【问题描述】:
有一个功能:
Func<object, double> calculation = x => Row("1") + Row("2");
其中 Row 是一些提取数据的函数,例如:
public static double Row(string rowName)
{
return 100; // just an example irrelevant in this case
}
我想提取 Row 函数调用的所有参数。在这种情况下
var parameters = Parse(calculation).ToList();
参数将包含“1”和“2”。我已经开始了:
private IEnumerable<string> Parse(Func<object, double> calculation)
{
// and I'm stuck here :)
}
我想提取的是值“1”和“2”,目前我被卡住了。任何想法如何解析这样的函数?
【问题讨论】:
-
请提供更多细节。
Row的范围到底是什么?你的意思是成为Row的成员x? -
Row 只是一些函数
public double Row(string rowName){ return 100; }Row 只是一个外部函数,很可能是静态的。如果有帮助,它可能是 x 的成员。现在它只是一个概念:) -
理论上可以通过检查 IL,但你最好在这里使用表达式树。
-
我在考虑表达式树,但我坚持
-
那么你的代码在语法上似乎是正确的;但我不清楚预期的行为是什么。按照您的示例,
calculation将为每个参数返回200。