0

i have a base class that accepts a StackFrame parameter.

public ExcpM(string Msg, StackFrame parCallStack = null, bool show = true)
{
    StackFrame LcallStack;
    if (parCallStack == null)
       LcallStack= new StackFrame(1, true);
    else LcallStack= parCallStack;

        //do stuff with info in LcallStack ..
}

originally i have started without the said parameter, i have added it because when using a common Extension method or any method that calls the above constructor, without the added parameter callStack.GetMethod() will then give the extension method data rather the intended caller which is the one i was after.

now that i called it as a solution, there comes a new problem that i am stuck without a proper solution for, which starts me thinking i am using it incorrectly.

the problem is when i created a derived class from the above ExcpM main class which also gives the wrong guy data (the derived own ctor..)

derived (added a false return value for Boolean methods usage)

public class ExcpFlsV:ExcpM
{
    bool Rtrn { get { return false; } }
    public ExcpFlsV(string bsMsg, StackFrame bsCallStack = null)
      : base(bsMsg, bsCallStack)
    {

    }
}

usage

public bool someMethodReturnsBool()
{
    //unless adding Local callStack field to every method passes ctor of ExcpFlsV as calling method
    StackFrame LcallStack = new System.Diagnostics.StackFrame(1, true);
    if(some condition no met) 
      return new ExcpFlsV("some error message...", LcallStack).Rtrn;
}

what is the proper way to pass the caller method as the data ?

Robb_2015
  • 377
  • 1
  • 3
  • 9

1 Answers1

0
public bool SecondLevel(string caller, int callerLvl = 2)
{
    return testStacktrace(" called by SecondLevel " + caller, callerLvl);
}
public bool testStacktraceFirstLvl(string caller, int callerLvl = 1)
{

    new ExcpM("testStacktrace " + caller, callerLvl); return false;

}

public ExcpM(string Msg,int CallerLevel=1, bool show = true)
{
    StackFrame callStack = new StackFrame(CallerLevel, true);
    this.lnNum = callStack.GetFileLineNumber();
    this.fl = callStack.GetFileName();
    this.mthod = callStack.GetMethod().Name;
    this.lMsg = Msg;
}

or with derived (False version) , skipping an extra Stack Frame

public class ExcpFlsV:ExcpM
{
    bool Rtrn { get { return false; } }
    public ExcpFlsV(string bsMsg, int CallerLevel=2)
        : base(bsMsg, CallerLevel)
    {

    }
}

public bool SecondLevel(string caller, int callerLvl = 3)
{
    return testStacktrace(" called by SecondLevel " + caller, callerLvl);
}
public bool testStacktraceFirstLvl(string caller, int callerLvl = 2)
{
  return new ExcFlsBlV("testStacktrace " + caller, calLvl).Rtrn;
}
Robb_2015
  • 377
  • 1
  • 3
  • 9