In real life this means that you can programmatically access all the methods that are currently executing leading you to where you are now in your code. Like this, for instance:
System.Diagnostics.StackTrace st=new System.Diagnostics.StackTrace();
Console.WriteLine(st.FrameCount.ToString());
for(int i=0;i<st.framecount;i++)
Console.WriteLine(st.GetFrame(i).GetMethod().Name);
Now, when I first stumbled across this, I instantly had a gut-feeling that this would come in handy some time. First thing that naturally pops into mind is error-handling and logging - but then it occurred to me "hey, you already get the stack-trace when there's an exception".
Somehow I still can't let it totally go - it must be useful for something else - perhaps in an extremely modularized system - or maybe for some advanced unit-testing. Anyone has any ideas?
No comments:
Post a Comment