C # obtain the current method and method call chain

Source: Internet
Author: User

Depth indicates the Backtracking depth of the method.

For example, if method A calls Method B and method B calls getcurrentmethodfullname (2), the result is the full name of method A (namespace + class name + method name)

To obtain the current method, the value of depth must be 0.

Internal static string getcurrentmethodfullname (INT depth)
{
Try
{
Stacktrace ST = new stacktrace ();
String methodname = ST. getframe (depth). getmethod (). Name;
String classname = ST. getframe (depth). getmethod (). declaringtype. tostring ();
Return classname + "." + methodname;
}
Catch
{
Return NULL;
}
}

 

 

If you do not need to know the method call chain, you can use methodbase. getcurrentmethod () to get the current method. You can use name to get the name and declaringtype. tostring () to get the namespace and class names.

 

Methodinfo method = (methodinfo) methodbase. getcurrentmethod ();

 

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.