如何獲取匿名方法的源代碼?匿名方法源代碼
例如:
Func<Boolean> func = (() => DateTime.Now.Seconds % 2 == 0);
Console.WriteLine(GetSourceCode(func)); // must: DateTime.Now.Seconds % 2 == 0
String GetSourceCode<T>(Func<T> f) - ???
如何獲取匿名方法的源代碼?匿名方法源代碼
例如:
Func<Boolean> func = (() => DateTime.Now.Seconds % 2 == 0);
Console.WriteLine(GetSourceCode(func)); // must: DateTime.Now.Seconds % 2 == 0
String GetSourceCode<T>(Func<T> f) - ???
你可以用它裏面表達並在其上調用toString(),這將讓你的源代碼。
像這樣:
Expression<Func<Boolean>> func = (() => DateTime.Now.Seconds % 2 == 0);
var str = func.ToString();
輸出海峽成爲 () => DateTime.Now.Seconds % 2 == 0
+1太棒了,不知道這個!輸出是'()=> DateTime.Now.Second%2 == 0' – 2013-03-02 13:21:06
非常感謝你 – 2013-03-02 13:33:33
+1真棒,簡單! – 2013-03-02 14:30:26
所以'GetSourceCode'應該返回一個'string'?我能問你爲什麼需要它嗎? – 2013-03-02 12:15:19
你不能,如果你喜歡,你只能得到'MSIL'和反編譯。這不是蘭達表情。 – 2013-03-02 12:18:21
是GetSourceCode返回String。 – 2013-03-02 12:20:53