583

登录 C# 时,如何知道调用当前方法的方法的名称?我知道一切System.Reflection.MethodBase.GetCurrentMethod(),但我想在堆栈跟踪中更进一步。我考虑过解析堆栈跟踪,但我希望找到一种更清晰更明确的方式,比如Assembly.GetCallingAssembly()but for 方法。

4

19 回答 19

594

试试这个:

using System.Diagnostics;
// Get call stack
StackTrace stackTrace = new StackTrace(); 
// Get calling method name
Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);

单线:

(new System.Diagnostics.StackTrace()).GetFrame(1).GetMethod().Name

它来自Get Calling Method using Reflection [C#]

于 2008-10-05T13:33:34.003 回答
448

在 C# 5 中,您可以使用caller info获取该信息:

//using System.Runtime.CompilerServices;
public void SendError(string Message, [CallerMemberName] string callerName = "") 
{ 
    Console.WriteLine(callerName + "called me."); 
} 

您还可以获得[CallerFilePath]and [CallerLineNumber]

于 2012-03-08T16:55:42.897 回答
121

您可以使用呼叫者信息和可选参数:

public static string WhoseThere([CallerMemberName] string memberName = "")
{
       return memberName;
}

该测试说明了这一点:

[Test]
public void Should_get_name_of_calling_method()
{
    var methodName = CachingHelpers.WhoseThere();
    Assert.That(methodName, Is.EqualTo("Should_get_name_of_calling_method"));
}

虽然 StackTrace 在上面运行得非常快,并且在大多数情况下不会成为性能问题,但调用者信息仍然快得多。在 1000 次迭代的样本中,我将其计时速度提高了 40 倍。

于 2012-11-21T15:07:04.090 回答
82

快速回顾这两种方法,速度比较是重要部分。

http://geekswithblogs.net/BlackRabbitCoder/archive/2013/07/25/c.net-little-wonders-getting-caller-information.aspx

在编译时确定调用者

static void Log(object message, 
[CallerMemberName] string memberName = "",
[CallerFilePath] string fileName = "",
[CallerLineNumber] int lineNumber = 0)
{
    // we'll just use a simple Console write for now    
    Console.WriteLine("{0}({1}):{2} - {3}", fileName, lineNumber, memberName, message);
}

使用堆栈确定调用者

static void Log(object message)
{
    // frame 1, true for source info
    StackFrame frame = new StackFrame(1, true);
    var method = frame.GetMethod();
    var fileName = frame.GetFileName();
    var lineNumber = frame.GetFileLineNumber();

    // we'll just use a simple Console write for now    
    Console.WriteLine("{0}({1}):{2} - {3}", fileName, lineNumber, method.Name, message);
}

两种方法的比较

Time for 1,000,000 iterations with Attributes: 196 ms
Time for 1,000,000 iterations with StackTrace: 5096 ms

所以你看,使用属性要快得多!事实上,速度快了近 25 倍。

于 2015-11-26T12:55:06.587 回答
68

我们可以通过仅实例化我们实际需要的帧而不是整个堆栈来稍微改进阿萨德先生的代码(当前接受的答案):

new StackFrame(1).GetMethod().Name;

这可能会表现得更好一些,尽管很可能它仍然必须使用完整的堆栈来创建单个帧。此外,它仍然有 Alex Lyman 指出的相同警告(优化器/本机代码可能会破坏结果)。最后,您可能需要检查以确保返回new StackFrame(1).GetFrame(1)不返回null,尽管这种可能性看起来不太可能。

看到这个相关的问题: 你可以使用反射来找到当前正在执行的方法的名称吗?

于 2008-10-05T19:16:48.897 回答
61

一般来说,你可以使用System.Diagnostics.StackTrace类来获取一个System.Diagnostics.StackFrame,然后使用GetMethod()方法来获取一个System.Reflection.MethodBase对象。但是,这种方法有一些注意事项

  1. 它代表运行时堆栈——优化可以内联一个方法,您不会在堆栈跟踪中看到该方法。
  2. 不会显示任何本机框架,因此如果您的方法甚至有可能被本机方法调用,这将不起作用,实际上目前没有可用的方法来做到这一点。

注意:我只是在扩展Firas Assad 提供的答案。)

于 2008-10-05T13:52:50.360 回答
49

从 .NET 4.5 开始,您可以使用调用者信息属性:

  • CallerFilePath- 调用函数的源文件;
  • CallerLineNumber- 调用函数的代码行;
  • CallerMemberName- 调用函数的成员。

    public void WriteLine(
        [CallerFilePath] string callerFilePath = "", 
        [CallerLineNumber] long callerLineNumber = 0,
        [CallerMemberName] string callerMember= "")
    {
        Debug.WriteLine(
            "Caller File Path: {0}, Caller Line Number: {1}, Caller Member: {2}", 
            callerFilePath,
            callerLineNumber,
            callerMember);
    }
    

 

此功能也存在于“.NET Core”和“.NET Standard”中。

参考

  1. Microsoft - 来电者信息 (C#)
  2. 微软 -CallerFilePathAttribute
  3. 微软 -CallerLineNumberAttribute
  4. 微软 -CallerMemberNameAttribute
于 2015-10-20T13:15:27.737 回答
16

显然这是一个迟到的答案,但如果您可以使用 .NET 4.5 或更高版本,我有更好的选择:

internal static void WriteInformation<T>(string text, [CallerMemberName]string method = "")
{
    Console.WriteLine(DateTime.Now.ToString() + " => " + typeof(T).FullName + "." + method + ": " + text);
}

这将打印当前日期和时间,然后是“Namespace.ClassName.MethodName”并以“:text”结尾。
样本输出:

6/17/2016 12:41:49 PM => WpfApplication.MainWindow..ctor: MainWindow initialized

样品用途:

Logger.WriteInformation<MainWindow>("MainWindow initialized");
于 2016-06-17T15:47:38.097 回答
14

请注意,由于优化,这样做在发布代码中是不可靠的。此外,在沙盒模式(网络共享)下运行应用程序根本不允许您抓取堆栈帧。

考虑面向方面的编程(AOP),例如PostSharp,它不是从您的代码中调用,而是修改您的代码,因此始终知道它在哪里。

于 2008-10-05T13:36:31.393 回答
9
/// <summary>
/// Returns the call that occurred just before the "GetCallingMethod".
/// </summary>
public static string GetCallingMethod()
{
   return GetCallingMethod("GetCallingMethod");
}

/// <summary>
/// Returns the call that occurred just before the the method specified.
/// </summary>
/// <param name="MethodAfter">The named method to see what happened just before it was called. (case sensitive)</param>
/// <returns>The method name.</returns>
public static string GetCallingMethod(string MethodAfter)
{
   string str = "";
   try
   {
      StackTrace st = new StackTrace();
      StackFrame[] frames = st.GetFrames();
      for (int i = 0; i < st.FrameCount - 1; i++)
      {
         if (frames[i].GetMethod().Name.Equals(MethodAfter))
         {
            if (!frames[i + 1].GetMethod().Name.Equals(MethodAfter)) // ignores overloaded methods.
            {
               str = frames[i + 1].GetMethod().ReflectedType.FullName + "." + frames[i + 1].GetMethod().Name;
               break;
            }
         }
      }
   }
   catch (Exception) { ; }
   return str;
}
于 2010-05-28T20:08:14.047 回答
7

也许你正在寻找这样的东西:

StackFrame frame = new StackFrame(1);
frame.GetMethod().Name; //Gets the current method name

MethodBase method = frame.GetMethod();
method.DeclaringType.Name //Gets the current class name
于 2008-10-05T20:09:42.160 回答
4
private static MethodBase GetCallingMethod()
{
  return new StackFrame(2, false).GetMethod();
}

private static Type GetCallingType()
{
  return new StackFrame(2, false).GetMethod().DeclaringType;
}

精彩的课程在这里:http ://www.csharp411.com/c-get-calling-method/

于 2011-03-04T18:40:54.570 回答
3

要获取方法名称和类名称,请尝试以下操作:

    public static void Call()
    {
        StackTrace stackTrace = new StackTrace();

        var methodName = stackTrace.GetFrame(1).GetMethod();
        var className = methodName.DeclaringType.Name.ToString();

        Console.WriteLine(methodName.Name + "*****" + className );
    }
于 2018-12-27T08:36:00.687 回答
2

查看.NET 中的 Logging 方法名称。小心在生产代码中使用它。StackFrame 可能不可靠...

于 2008-10-05T13:36:42.937 回答
2

我使用的另一种方法是向相关方法添加参数。例如,代替void Foo(),使用void Foo(string context)。然后传入一些指示调用上下文的唯一字符串。

如果您只需要调用者/上下文进行开发,您可以param在发货前删除。

于 2008-10-16T01:06:21.327 回答
2

Firas Assaad 回答的额外信息。

new StackFrame(1).GetMethod().Name;在 .net core 2.1 中使用了依赖注入,并且我将调用方法作为“开始”。

我试过了[System.Runtime.CompilerServices.CallerMemberName] string callerName = "" ,它给了我正确的调用方法

于 2018-12-27T09:19:17.327 回答
1
StackFrame caller = (new System.Diagnostics.StackTrace()).GetFrame(1);
string methodName = caller.GetMethod().Name;

就够了,我想。

于 2012-12-14T08:20:41.080 回答
1

我们也可以使用 lambda 来查找调用者。

假设您有一个由您定义的方法:

public void MethodA()
    {
        /*
         * Method code here
         */
    }

你想找到它的来电者。

1 . 更改方法签名,以便我们有一个 Action 类型的参数(Func 也可以使用):

public void MethodA(Action helperAction)
        {
            /*
             * Method code here
             */
        }

2 . Lambda 名称不是随机生成的。规则似乎是: > <CallerMethodName>__X 其中 CallerMethodName 被前一个函数替换, X 是一个索引。

private MethodInfo GetCallingMethodInfo(string funcName)
    {
        return GetType().GetMethod(
              funcName.Substring(1,
                                funcName.IndexOf("&gt;", 1, StringComparison.Ordinal) - 1)
              );
    }

3 . 当我们调用 MethodA 时,Action/Func 参数必须由调用方方法生成。例子:

MethodA(() => {});

4 . 在 MethodA 中,我们现在可以调用上面定义的辅助函数并找到调用方方法的 MethodInfo。

例子:

MethodInfo callingMethodInfo = GetCallingMethodInfo(serverCall.Method.Name);
于 2013-05-30T15:43:07.947 回答
-1
var callingMethod = new StackFrame(1, true).GetMethod();
string source = callingMethod.ReflectedType.FullName + ": " + callingMethod.Name;
于 2015-07-06T09:02:13.163 回答