是否可以确定PostError中的调用方法名称“吃披萨”? 我想我可以通过“EatPizza”作为参数之一,但每次方法名称更改时都需要更改(不必要的维护)。但之后,我甚至无法在“EatPizza”的上下文中找到方法名称“EatPizza”(使用stacktrace,getframe,getmethod)。获取方法被调用的方法?
public void EatPizza(Pizza p){
if(p==null){ //A arbitrary made up error
Utilities.PostError();
}
else{
p.Slices -= 1;
}
}
...
public void PostError(){
//Basically posting to database the name of the method
//Tried this, didn't work: (new StackTrace(true)).GetFrame(5).GetMethod().Name
//Is it possible to determine the calling method name "Eat Pizza" in this context?
}
当我尝试不同的值(0至StackTrace.FrameCount-1)StackTrace.GetFrame,我得到下面的值,如果我只是想 “EatPizza”:
.ctor
ThreadStart
Main
_nExecuteAssembly
RunUsersAssemblyDebugInZone
“没有工作”以什么方式? – 2011-04-14 17:51:26
我添加了一个附录 – sooprise 2011-04-14 17:53:13
你知道,抛出一个异常会得到堆栈跟踪,并且是推荐的处理错误的方法。你有没有这样做的理由?如果需要,您可以在catch块中调用PostError。 – driis 2011-04-14 17:53:33