C#方法调用者方法、调用者

由网友(挺like的)分享简介:可能重复: How我能找到调用当前方法的方法? Possible Duplicate:How can I find the method that called the current method? 你好,如何从方法中确定的方法的调用者?例如:Hi,how can i determine the call...

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

Possible Duplicate: How can I find the method that called the current method?

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

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:

http://iridescence.no/post/GettingtheCurrentStackTrace.aspx http://blogs.msdn.com/jmstall/存档/ 2005/03/20 / 399287.aspx http://iridescence.no/post/GettingtheCurrentStackTrace.aspx 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);
阅读全文

相关推荐

最新文章