
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Writing a More Flexible StackTrace Class
|
293
Discussion
This recipe uses the System.Diagnostics.StackTrace object to obtain a list of stack
frames, which it then provides to the user. The
StackTrace class provides a conve-
nient way to obtain a stack trace, an exception object, or a specific thread from the
current point in code. Unfortunately, the
StackTrace provides only a very simplified
way to get at each stack frame. It would be much better if the
StackTrace object oper-
ated like a collection. To make this happen, you can create an intermediate object
called
StackTraceList that inherits from StackTrace and implements the ICloneable,
IList, ICollection, and IEnumerable interfaces.
The constructors for the
StackTraceList class mimic the StackTrace constructors.
Each
StackTraceList constructor passes its work along to the base class using the
base keyword:
public StackTraceList( ) : base( )
Each StackTraceList constructor contains a call to the private method, Init-
InternalFrameArray
. This private method copies all of the individual StackFrame
objects from the base StackTrace object into a private field of type StackFrame[]
called internalFrameArray. The StackTraceList uses the internalFrameArray field as a
convenient storage mechanism for each individual
StackFrame object; in addition,