c# - How to find the method in which an anonymous delegate was declared -


i'm trying perhaps dubious caller analysis logging. regardless of if want in end, i'd know if i'm looking possible @ all. if not, why not? since seems should possible me.

specifically:

    private static action createanonymousmethod()     {         methodbase declaringmethod = new stackframe(true).getmethod();          return delegate()         {             var myframe = new stackframe(true);              // *this* anonymous method. can figure out name             // "<createanonymousmethod>garbage" when compiler generated             // it, knew answer, methodbase itself, can find             // no path declaringmethod. answer incorrect,             // feel data should around here somewhere.             methodbase whatgoeshere = myframe.getmethod();              assert.areequal(declaringmethod, whatgoeshere);         };     } 

my goal want more or less 'log' method can walk stack , find caller. know [callermembername] attributes , such, there no corresponding attribute declaring type of caller's member, , caller's member name mere string. i'd find proper memberinfo if possible.

so led me finding caller's method via stackframe.getmethod() nicer logging i'd detect if it's anonymous delegate, , instead of or in addition anonymous delegate's memberinfo, use declaring method's memberinfo.

however, seems can't 'get there here.' frustrating bit know compiler knew @ least while compiling, given name of anonymous delegate, can't seem find way memberinfo @ runtime.

it seems want invented property memberinfo.declaringmember anonymous method return member in declared. saying realize anonymous method isn't clr thing, , time you're looking @ reflection information flat gone. hoping in compilergeneratedattribute hold onto original source memberinfo , extract attribute, attribute seems leave no trace of it.

at point think option brittle attempt parse anonymous method name (ha!) , use somehow search corresponding memberinfo.

anyway, before give , go down route , see how terrible is, thought i'd throw hail mary out fine folks here @ stackoverflow , see if there's genius solution i'm missing, or, if thought process fundamentally flawed.


Comments