Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

source code of anonymous method

How get a source code of anonymous method?

For example:

Func<Boolean> func = (() => DateTime.Now.Seconds % 2 == 0);

Console.WriteLine(GetSourceCode(func)); // must: DateTime.Now.Seconds % 2 == 0

String GetSourceCode<T>(Func<T> f) - ???
like image 959
Horev Ivan Avatar asked Nov 21 '25 12:11

Horev Ivan


1 Answers

You can wrap it inside Expression and call ToString() on it and that will get you the source code.

Like this:

Expression<Func<Boolean>> func = (() => DateTime.Now.Seconds % 2 == 0);
var str = func.ToString();

The output str becomes () => DateTime.Now.Seconds % 2 == 0

like image 153
Davor Zlotrg Avatar answered Nov 23 '25 03:11

Davor Zlotrg