I have this code:
class Program
{
static void Main(string[] args)
{
Action whatToDo = () => {
var member = (MemberInfo)(MethodBase.GetCurrentMethod());
Thread.Sleep(0); //whatever, need something to put a breakpoint on
};
whatToDo();
}
}
when I run it and use watch to look inside the object bound to member reference I see that MemberInfo.Name property has value <Main>b__0.
This looks weird. Why wouldn’t reflection make use of whatToDo name? What if I had more that one action with the same signature inside one member function – how would I tell which one is reported?
Why is such a weird name returned by reflection?
Lambda expressions which are being converted to delegates are transformed into methods. Your code is equivalent to:
… except that the compiler is smart enough to create new classes where necessary for captured variables etc. While in my transformation the extra method is called
MyLambda, the C# compiler generates unspeakable names which aren’t valid C# identifiers (to avoid collisions, prevent you from accessing them directly etc).