是否可以扫描作为lambda表达式提供的函数,以在运行时确定函数的性质?
例:
class Program
{
static void Main(string[] args)
{
Examples example = new Examples(x => x ^ 2 + 2);
}
}
public class Examples
{
public Examples(Func<dynamic, dynamic> func)
{
// How can I scan "func" here to figure out that it is defined as "x => x ^ 2 + 2" instead of, say, as "x => Math.Exp(x)"?
}
}
最佳答案
您需要使用expression trees,如下所示:
public Examples(Expression<Func<dynamic, dynamic>> func) {
...
}
有关更多信息,请参见here。
关于c# - 如何在C#中“扫描” lambda表达式?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1996975/