开发者

Getting The Method That A Method Was Called From? [duplicate]

This question already has answers here: How can I find the method that called the current method? (17 answers) Closed 6 years ago.

Is it possible to determine the calling method name "Eat Pizza" in PostError? I 开发者_运维知识库guess I could pass "EatPizza" as one of the arguments, but that would require changes each time the method name changes (unnecessary maintenance). But then, I wasn't even able to find the method name "EatPizza" in the context of "EatPizza" (using 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?
}

When I try different values (0 to StackTrace.FrameCount-1) in StackTrace.GetFrame, I get the following values, when I just want "EatPizza":

.ctor
ThreadStart
Main
_nExecuteAssembly
RunUsersAssemblyDebugInZone


You were on the right track with creating a StackTrace object, but you seem to have misunderstood the argument to GetFrame. Frames are numbered from the bottom-most frame, so:

  • GetFrame(0) would return PostError
  • GetFrame(1) would return the caller of PostError

So just try this:

var trace = new StackTrace(true);
WriteToDB(trace.GetFrame(1).GetMethod().Name);

Personally, I would prefer to get the entire stack trace rather than just the caller, so I'd do this:

var trace = new StackTrace(true);
WriteToDB(trace.ToString());


Is it possible to determine the calling method name "Eat Pizza" in PostError? I guess I could pass "EatPizza" as one of the arguments, but that would require changes each time the method name changes (unnecessary maintenance).

Calling PostError in all the methods in which something could go wrong is also "unnecessary maintenance". It also complicates the execution flow of your program, because you will have to check for errors all over the place, and high-level processes will have to check if the low level processes completed successfully.

It is better to use the exception handling structures provided by the CLR and C#.

The exact location in which the error occured is stored in the exception's StackTrace property.

pubic void BigDinnerEatingProcess()
{
    try
    {
         WhateverHappensAtTheTopLevel();
    }
    catch (PizzaNotDeliveredException ex)
    {
         Utilities.PostError(ex);
         MessageBox.Show("Dinner was not eaten. Please make sure the pizza is delivered.");
    }
}

public void EatPizza(Pizza p)
{
    if (p == null)
        throw new PizzaNotDeliveredException();
    p.RemoveOneSlice();
}

private void PostError(Exception ex)
{
    string errorLocation = ex.StackTrace;
    //...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜