Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何在运行时从方法中找到调用方法的方法名?_.net_Reflection - Fatal编程技术网

.net 如何在运行时从方法中找到调用方法的方法名?

.net 如何在运行时从方法中找到调用方法的方法名?,.net,reflection,.net,Reflection,如何在运行时从方法中找到调用方法的方法名 例如: Class A { M1() { B.M2(); } } class B { public static M2() { // I need some code here to find out the name of the method that // called this method, preferably the name of the decla

如何在运行时从方法中找到调用方法的方法名

例如:

Class A
{
    M1()
    {
        B.M2();
    }
}

class B
{
    public static M2()
    {
        // I need some code here to find out the name of the method that
        // called this method, preferably the name of the declared type
        // of the calling method also.
    }
}
您可以尝试:

using System.Diagnostics;

StackTrace stackTrace = new StackTrace();
Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);

我认为您正在寻找:

using System.Diagnostics;

StackTrace stackTrace = new StackTrace();
stackTrace.GetFrame(1).GetMethod().Name;

您可以通过显示调用堆栈来实现这一点,如下面的代码所示。这将找到整个调用堆栈,而不仅仅是调用方法

void displaycallstack() {
    byte[] b;
    StackFrame sf;
    MemoryStream ms = new MemoryStream();
    String s = Process.GetCurrentProcess().ProcessName;
    Console.Out.WriteLine(s + " Call Stack");
    StackTrace st = new StackTrace();
    for (int a = 0;a < st.FrameCount; a++) {
        sf = st.GetFrame(a);
        s = sf.ToString();
        b = Encoding.ASCII.GetBytes(s);
        ms.Write(b,0,b.Length); 
    }
    ms.WriteTo(System.Console.OpenStandardOutput());
}
void displaycallstack(){
字节[]b;
堆垛机;
MemoryStream ms=新的MemoryStream();
字符串s=Process.GetCurrentProcess().ProcessName;
Console.Out.WriteLine(s+“调用堆栈”);
StackTrace st=新的StackTrace();
对于(int a=0;a
检查System.Diagnostics.Trace类,但据我所知,在使用它时,性能价格

最好不要使用StackFrame,因为存在一些.NET安全问题。如果代码不完全可信,表达式“new StackFrame()”将引发安全异常

要获取当前方法,请使用:

MethodBase.GetCurrentMethod().Name


关于调用方法,请参阅堆栈溢出问题。

认为需要实现类似的功能通常是应用程序设计中存在缺陷的一个好迹象。最好回头看看设计,看看是否可以先解决这个问题。但是,事实上,这并不总是绝对正确的。你为什么要这样做?重复:@Jon:我自己从来没有做过,尽管我已经接近了好几次。当试图理解刚刚落在你盘子上的一堆热气腾腾的东西时,这是一种有用的技术。调试器知道如何做-为什么其他人需要知道如何做?哇,这可能是我见过的任何人编写控制台的最有趣的方式:-)这看起来像是共谋者代码。。。乔恩,仔细看了代码之后。。我不得不同意。它只是从一个关于如何显示堆栈跟踪的示例中获取的。下次我得找更好的例子:)谢谢你,但我想你没抓住重点。我不需要当前方法名,我需要调用当前方法的方法名。:)我最近问了同样的问题;)StackFrame有很多问题-如果调用方法被JIT内联,您将得到错误的结果-1个投票率更高的旧项目的副本(以及-1d相同基础上的问题)@Ruben-从未意识到有人以比你应得的否决票更快的速度发布答案!我明白,这不是第一次有人拒绝我的分析。如果答案没有获得赞成票,我就不会投反对票。我知道这是荒谬的。(另一个问题有更好的答案——我正在寻找一个涵盖NoInLine点和语法的答案)。(我个人要么删除我创建的DUP,要么编辑它们,使它们变得不同和/或比其他答案更好-如果其他人在我之前覆盖了相同的领域,我很高兴被指出和/或被否决)。嘿,weichsel这一个很好+1从我这边。。!!虽然这个线程有点旧,但绝对有用..!!:)请注意,如果您在发行版中编译,这可能不会给出相同的结果,因为JIT将内联/尾部调用您的方法(如果可以的话)。