C# 获得调用者信息

public static void Run()
{
    //跟踪侦听
    System.Diagnostics.Trace.Listeners.Add(new TextWriterTraceListener(System.Console.Out));

    TraceMessage("First");
    TraceMessage("Second");
    TraceMessage("Third");
}

public static void TraceMessage(string? message = null,
    //获取方法调用方的方法或属性名称
    [CallerMemberName] string? memberName = "",
    //获取包含调用方的源文件的完整路径
    [CallerFilePath] string? sourceFilePath = "",
    //获取源文件中调用方法的行号
    [CallerLineNumber] int sourceLineNumber = 0)
{
    Trace.WriteLine("message: " + message);
    Trace.WriteLine("member name: " + memberName);
    Trace.WriteLine("source file path: " + sourceFilePath);
    Trace.WriteLine("source line number: " + sourceLineNumber);
}

示例代码

CallerTestDemo

参考资料

CallerFilePathAttribute 类
CallerLineNumberAttribute 类
CallerMemberNameAttribute 类

posted @ 2022-03-03 19:32  Lulus  阅读(436)  评论(0编辑  收藏  举报