9.5. A More Flexible StackTrace Class

Problem

You have a StackTrace class containing a listing of stack frames. You need to iterate through these stack frames as if you were using an Array type object.

Solution

Use the adapter design pattern to adapt the public interface of a StackTrace object to look like a Collection type object. The StackTraceArray class implements this design pattern:

using System; using System.Collections; using System.Diagnostics; using System.Reflection; using System.Text; using System.Threading; public class StackTraceArray : StackTrace, IList { public StackTraceArray( ) : base( ) { InitInternalFrameArray( ); } public StackTraceArray(bool needFileInfo) : base(needFileInfo) { InitInternalFrameArray( ); } public StackTraceArray(Exception e) : base(e) { InitInternalFrameArray( ); } public StackTraceArray(int skipFrames) : base(skipFrames) { InitInternalFrameArray( ); } public StackTraceArray(StackFrame frame) : base(frame) { InitInternalFrameArray( ); } public StackTraceArray(Exception e, bool needFileInfo) : base(e, needFileInfo) { InitInternalFrameArray( ); } public StackTraceArray(Exception e, int skipFrames) : base(e, skipFrames) { InitInternalFrameArray( ); } public StackTraceArray(int skipFrames, bool needFileInfo) : base(skipFrames, needFileInfo) { InitInternalFrameArray( ); } public StackTraceArray(Thread targetThread, bool needFileInfo) : base(targetThread, needFileInfo) { InitInternalFrameArray( ); } public StackTraceArray(Exception e, int skipFrames, ...

Get C# Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.