How to obtain information about the caller in the called Method

Source: Internet
Author: User

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 );
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.