你必須使用Func
或Action
,但是你可以使用它在發送方,而不是方法方面,所以你還是可以接受任何類型。
static void Main()
{
Foo foo1 = null;
Bar bar2 = null;
Examine<Foo>(x => (Func<int,bool>)foo1.Test1);
Examine<Bar>(x => (Func<string,string,string>)bar2.DifferentMethod2);
}
public static void Examine<T>(Expression<Func<T, Delegate>> expression2)
{
// examine expression tree to get method name (MethodInfo)
}
這產生像
.Lambda #Lambda1<System.Func`2[SandboxConsole.Foo,System.Delegate]>(SandboxConsole.Foo $x) {
(System.Func`2[System.Int32,System.Boolean]).Call .Constant<System.Reflection.MethodInfo>(Boolean Test1(Int32)).CreateDelegate(
.Constant<System.Type>(System.Func`2[System.Int32,System.Boolean]),
.Constant<SandboxConsole.Program+<>c__DisplayClass0_0>(SandboxConsole.Program+<>c__DisplayClass0_0).foo1)
}
和
.Lambda #Lambda1<System.Func`2[SandboxConsole.Bar,System.Delegate]>(SandboxConsole.Bar $x) {
(System.Func`3[System.String,System.String,System.String]).Call .Constant<System.Reflection.MethodInfo>(System.String DifferentMethod2(System.String, System.String)).CreateDelegate(
.Constant<System.Type>(System.Func`3[System.String,System.String,System.String]),
.Constant<SandboxConsole.Program+<>c__DisplayClass0_0>(SandboxConsole.Program+<>c__DisplayClass0_0).bar2)
}
兩個調用一個表達式。
還沒有測試過,但func或動態動態?庸醫像鴨子一樣 –