Asp. NET implement get Call Method name _ Practical Tips

Source: Internet
Author: User
Tags reflection

The example in this article describes the technique for obtaining the calling method name in asp.net. Share to everyone for your reference. The implementation methods are as follows:

In the Write log function, you need to record the log caller's module name, namespace name, class name, and method name, think of the use of reflection (involving reflection, pay attention to performance), but the specific is not understood, so search, sorted as follows:


You need to add the appropriate namespaces:

Copy Code code as follows:
Using System;
Using System.Diagnostics;
Using System.Reflection;

If you are only getting the current method name, you can use the following code:
Copy Code code as follows:
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 namespaces
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 ();
}

Typically, however, you get the caller of this logging method, so you need to use the following code: (This method is demo only)

Copy Code code as follows:
public static void Writesyslog (string content)
{
const int level = 1000;

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

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, it's interesting that the caller of main

Copy Code code as follows:
system.appdomain._nexecuteassembly (Assembly Assembly, string[] args)


Pass
Copy Code code as follows:
StackTrace ss = new StackTrace (true);
stackframe[] SFS = ss. Getframes ();

can be known. NET Program Execution order:
Copy Code code as follows:
System.Threading.ThreadHelper.ThreadStart ()
System.Threading.ExecutionContext.Run (ExecutionContext ExecutionContext, ContextCallback callback, Object State)
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly ()
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 can be positioned to System.Reflection.MethodBase view.

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

I hope this article will help you with the ASP.net program design.

Related Article

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.