how to get invoker class of this method

后端 未结 2 746
我在风中等你
我在风中等你 2021-01-14 11:39

is it possible?

i want to get the name of class (like foo) which is invoking my method (like myMethod)

(and the method is in another class(like i))

2条回答
  •  滥情空心
    2021-01-14 11:55

    I found the fallowing : http://msdn.microsoft.com/en-us/library/hh534540.aspx

    // using System.Runtime.CompilerServices 
    // using System.Diagnostics; 
    
    public void DoProcessing()
    {
        TraceMessage("Something happened.");
    }
    
    public void TraceMessage(string message,
            [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);
    }
    
    // Sample Output: 
    //  message: Something happened. 
    //  member name: DoProcessing 
    //  source file path: c:\Users\username\Documents\Visual Studio 2012\Projects\CallerInfoCS\CallerInfoCS\Form1.cs 
    //  source line number: 31
    

提交回复
热议问题