Someone mentioned this question in BBS just now. in fact, I had this idea a long time ago. but at that time, I only looked at the reflected class library and found that it could not be implemented. now it's actually easy to think about this, that is, to use the call stack to obtain this information. the call stack can be used to obtain a lot of useful information.
Since the code is relatively simple, I will not talk about it much:
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. reflection;
Using system. diagnostics;
Namespace windowsformsapplication1
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private void button#click (Object sender, eventargs E)
{
Mytest T = new mytest ();
T. Add (1 );
}
}
Public class mytest
{
Public void add (int I)
{
I ++;
Stacktrace Ss = new stacktrace (true );
Type T = ss. getframe (1). getmethod (). declaringtype;
MessageBox. Show (T. fullname );
}
}
}