本文介绍了C#方法调用者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  How我能找到调用当前方法的方法?

你好,如何从方法中确定的方法的调用者?例如:

Hi,how can i determine the caller of a method from within the method? Eg:

SomeNamespace.SomeClass.SomeMethod() {
   OtherClass();
}

OtherClass() {
   // Here I would like to able to know that the caller is SomeNamespace.SomeClass.SomeMethod
}

感谢

推荐答案

这些文章应该会有所帮助:

These articles should be of help:

  1. http://iridescence.no/post/GettingtheCurrentStackTrace.aspx
  2. http://blogs.msdn.com/jmstall/存档/ 2005/03/20 / 399287.aspx
  1. http://iridescence.no/post/GettingtheCurrentStackTrace.aspx
  2. http://blogs.msdn.com/jmstall/archive/2005/03/20/399287.aspx

基本上,code是这样的:

Basically the code looks like this:

StackFrame frame = new StackFrame(1);
MethodBase method = frame.GetMethod();
message = String.Format("{0}.{1} : {2}",
method.DeclaringType.FullName, method.Name, message);
Console.WriteLine(message);

这篇关于C#方法调用者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:41
查看更多