I'm trying to get the very first method name that initialed multiple method calls.
for example I have the below,
static void Main(string[] args)
{
Test1();
}
static void Test1()
{
Test2();
}
static void Test2()
{
Console.WriteLine("Soemthing");
}
In the Test2 method, I need to know the Main Method has initiated this chain. I tried using the StackTrace and Reflection as below.
StackTrace stackTrace = new StackTrace();
MethodBase methodBase = stackTrace.GetFrame(1).GetMethod();
Console.WriteLine(methodBase.Name);
but the stackTrace.getFrame(1) is not what I expect. It will give me the previous method which is Test1.
I do not want to hard code the getFrame value to 2 to get the Main method.
Is there any way to get the parent method from any of the sub methods without hard coding the Frame value?
Aucun commentaire:
Enregistrer un commentaire