O'Reilly logo

C# Cookbook by Jay Hilyard, Stephen Teilhet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required