【发布时间】:2016-09-01 20:15:26
【问题描述】:
我需要创建一个动态检查作为参数传递的方法委托的方法。但我不能强制表达式树接受任何方法,无论它们的签名如何。
这是我的方法(无法编译:error CS0030: Cannot convert type 'method' to 'Delegate')
public void Examine<T>(Expression<Func<T, Delegate>> expression2)
{
// examine expression tree to get method name (MethodInfo)
}
我想这样调用它:
Examine<Foo>(x => foo1.Test1);
Examine<Bar>(x => bar2.DifferentMethod2);
// etc, with other methods
地点:
- 类 Foo 有方法:
bool Test1(int num) - 类 Bar 有方法:'string DifferentMethod2(string a, string b)`
- 还有许多其他
如何实现?
备注:
- 我不能使用 Func 或 Action,因为有许多可能的方法签名需要接受我不会引用的参数类型。
【问题讨论】:
-
没测试过,但是func或者action是动态的?像鸭子一样的嘎嘎声
标签: c# lambda expression-trees