Get call Method name in. NET

Source: Internet
Author: User

When writing the logging function, you need to record the log caller's module name, namespace name, class name, and method name, think of the use of reflection (please note the performance of the reflection), but specifically what is not understood, so search, organized as follows:

You need to add the appropriate namespaces:

Using system;using system.diagnostics;using System.Reflection;

If you are only getting the current method name, you can use the following code:

public static void Writesyslog (Int. level, string content) {    methodbase MB = Methodbase.getcurrentmethod ();    string systemmodule = Environment.NewLine;    Systemmodule + = "Module Name:" + MB. Module.tostring () + Environment.NewLine;    Systemmodule + = "namespace name:" + MB. Reflectedtype.namespace + Environment.NewLine;    Fully qualified name, including namespace    Systemmodule + = "Class Name:" + MB. Reflectedtype.fullname + Environment.NewLine;    Systemmodule + = "Method Name:" + MB. Name;    Console.WriteLine ("Logdate: {0}{1}level: {2}{1}systemmodule: {3}{1}content: {4}", DateTime.Now, Environment.NewLine, Level, systemmodule, content);    Console.WriteLine ();}

This address: http://www.cnblogs.com/Interkey/p/GetMethodName.html
In general, however, you get the caller of this logging method, so you need to use the following code: (This method is for demonstration only)

public static void Writesyslog (string content) {    Const int. level = +;    StackTrace ss = new StackTrace (true);    Index:0 is the method for itself, 1 is the calling method, 2 is the upper layer, and so on    methodbase MB = ss. GetFrame (1). GetMethod ();    This article address: http://www.cnblogs.com/Interkey/p/GetMethodName.html    stackframe[] SFS = ss. Getframes ();    string systemmodule = Environment.NewLine;    Systemmodule + = "Module Name:" + MB. Module.tostring () + Environment.NewLine;    Systemmodule + = "namespace name:" + MB. Declaringtype.namespace + Environment.NewLine;    Only class name    Systemmodule + = "Class Name:" + MB. Declaringtype.name + Environment.NewLine;    Systemmodule + = "Method Name:" + MB. Name;    Console.WriteLine ("Logdate: {0}{1}level: {2}{1}systemmodule: {3}{1}content: {4}", DateTime.Now, Environment.NewLine, Level, systemmodule, content);    Console.WriteLine ();}

For this, the interesting thing is the caller of Main, system.appdomain._nexecuteassembly (Assembly Assembly, string[] args).

Pass

StackTrace ss = new StackTrace (true); stackframe[] SFS = ss. Getframes ();

Can know. NET Program Execution order:

    1. System.Threading.ThreadHelper.ThreadStart ()
    2. System.Threading.ExecutionContext.Run (ExecutionContext ExecutionContext, ContextCallback callback, Object State)
    3. Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly ()
    4. system.appdomain._nexecuteassembly (Assembly Assembly, string[] args)

Then enter the method main.

In addition, many other properties can be obtained from the MethodBase class, and you can navigate to System.Reflection.MethodBase view yourself.

Using reflection, you can iterate through all the property names of the class, the method name, the member name, and one interesting little example: converting the value of a variable to the variable name itself by reflection

REFERENCE from:http://www.cnblogs.com/interkey/p/getmethodname.html

Get call Method name in. NET

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.