So in JavaScript, you can call toString() on a function to return it's source code. However, no such construct exists for C# delegates.
Let's say I have this code sample here:
void DoThing()
{
Console.WriteLine("I did a thing");
}
void Execute(Action act)
{
act();
}
public static int Main(string[] args)
{
Execute(DoThing);
Execute(() => Console.WriteLine("llama lambda"));
}
If I debug this code in Visual Studio 2019, and put a breakpoint on Execute
, I can look at the properties of act
.
For the first call with DoThing
, Visual Studio can at least give me the name of the method and it's return type (Void DoThing()
), but not the source code. If I have multiple DoThing
s in my source code, this will not be very helpful at all.
But for the lambda, I get no useful info in the debugger ({Method = {Void <Process>b__11_0()}}
).
Now, of course in this small example code, it is pretty easy to find the functions by just retracing the call stack, but in some cases, this is a very long and arduous endeavour, especially when it needs to be done multiple times.
I searched around on google and I couldn't find a simple answer to this question, or even find anywhere where this question was asked:
Is there any way to find the source code of a delegate in C# when using the Visual Studio debugger (without just retracing the call stack)?
If yes, how?
And if no, why?