Home > Backend Development > C++ > How Can I Get the Name of the Method That Called the Current Method in C#?

How Can I Get the Name of the Method That Called the Current Method in C#?

Susan Sarandon
Release: 2025-02-01 02:21:09
Original
943 people have browsed it

How Can I Get the Name of the Method That Called the Current Method in C#?

The method name of obtaining the call method in C#

C# developers often need to determine the method of calling the current execution method. Although can obtain the information of the current method, to obtain the information of the call method, you need to learn more about the stack tracking.

Traversing the stack tracking System.Reflection.MethodBase.GetCurrentMethod()

Analysis of the stack tracking seems feasible, but there is a more concise and direct method. You can get the information of the call set, and similar methods are also applicable to the method.

Traveling up the stack

Assembly.GetCallingAssembly()

Class can achieve this. Through examples such as this type and calling its

method, you can trace the stack first level and obtain the call method. Then, you can use the name of the call method. sample code

System.Diagnostics.StackTrace The following code demonstrates the usage of this technology: GetFrame(1) GetMethod().Name

or, you can simplify the entire process to a line of code:

The above is the detailed content of How Can I Get the Name of the Method That Called the Current Method in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template