是否可以在 PostError 中确定调用方法名称“Eat Pizza”?我想我可以将“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?
}
当我在 StackTrace.GetFrame 中尝试不同的值(0 到 StackTrace.FrameCount-1)时,我得到以下值,而我只想要“EatPizza”:
.ctor
ThreadStart
Main
_nExecuteAssembly
RunUsersAssemblyDebugInZone