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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.