PostErrorで呼び出しメソッド名「Eat Pizza」を特定することはできますか? 引数の 1 つとして "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