Living .NET...

Musings on .NET, and the like - Manoj G [MVP, Connected Systems Developer]

March 2004 - Posts

StackTrace and StackFrame

All of us would be aware of the Call Stack, which we usually inspect while debugging any type of application. It would be great if we could really inspect the Call Stack in some way in our code at runtime. System.Diagnostics namespace gives you just that in the form of two very useful classes : The StackFrame and the StackTrace.

A Stack frame is created when a method call is made. Typically, the stack frame contains the information related to the method call and this can be inspected by calling the GetMethod method of a StackFrame instance. This method just returns an instance of type MethodBase. Also, for debug builds, you can get information like source file name, line number etc.

StackTrace is nothing but a collection of StackFrame instances that are on the stack. One place where you would have used this is the StackTrace of the exception object which is just a string representation of the StackTrace. Infact, you can create a Stacktrace instance out of an exception object.

Here's a simple usage which just prints out the call stack till the current method:

StackTrace st  = new StackTrace();
for(int i  = 0; i < st.FrameCount ; i ++)
{
   Console.WriteLine(st.GetFrame(i).GetMethod().Name );
}

There is one small catch. As you all know, as a way of runtime optimization, the CLR may decide to inline a method call. In such cases, you will not have a Stack Frame created (there is no method call at all here).

I can think of few uses of Stacktrace: (There may be many more..)
1. Inspect the metadata of the caller of your method, maybe for security purposes.
2. Check the depth of a method call. This can be useful when the method call is recursive.
3. Log call patterns at runtime for analysis purposes.

 

Posted: Thu, Mar 25 2004 6:49 by Manoj G | with 3 comment(s)
Filed under:
Interface definition with no methods ?

Can an Interface definition have no methods at all ?  Something like:

Public Interface IBlank
 
' Am Empty
End Interface

If possible, what do you think can be the use of such interfaces? On a first thought, it might not make sense to have these. But actually, empty interfaces can be very handy. Implementing an empty interface is like qualifying that class with an attribute or saying that it supports certain semantics.

The same thing can be achieved though custom attributes but it would be a kind of an overkill to inspect the type metadata to see if the type supports an operation as against the simple usage of the TypeOf operator. So, it is better (easier) to have something like:

Public Interface ISomeOperation
End Interface

Public Class
MyType
 
Implements ISomeOperation

than  have

<SomeOperation>
Public Class MyType

You can come across many such interfaces in .NET BCL. INamingContainer and ILogicalThreadAffinative are examples of such interfaces.

Wait. Don’t think that attributes are completely replaceable with empty interfaces. Attributes are a lot more powerful, just by the virtue of being able to take many more parameters. So, in my opinion, if an attribute has none other than True or False, Yes or No kind of values, then an empty interface is better (easier to use).

Posted: Thu, Mar 4 2004 7:38 by Manoj G | with no comments
Filed under:
Picks of the Week

Just from this time's MSDN Flash..Good read..

1) Coding Best Practices Using DateTime in the .NET Framework

http://msdn.microsoft.com/netframework/default.aspx?pull=/library/en-us/dndotnet/html/datetimecode.asp

2) Secure Coding Guidelines for the .NET Framework

http://msdn.microsoft.com/security/securecode/dotnet/default.aspx?pull=/library/en-us/dnnetsec/html/seccodeguide.asp

Posted: Tue, Mar 2 2004 6:00 by Manoj G | with no comments
Filed under: