how to get invoker class of this method

后端 未结 2 747
我在风中等你
我在风中等你 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:54

    You can use StackTrace to work out the caller - but that's assuming there's no inlining going on. Stack traces aren't always 100% accurate. Something like:

    StackTrace trace = new StackTrace();
    StackFrame frame = trace.GetFrame(1); // 0 will be the inner-most method
    MethodBase method = frame.GetMethod();
    Console.WriteLine(method.DeclaringType);
    

提交回复
热议问题