using System.Diagnostics; namespace StardewModdingAPI.Framework.Deprecations { /// An immutable stack trace that caches its values. internal class ImmutableStackTrace { /********* ** Fields *********/ /// The underlying stack trace. private readonly StackTrace StackTrace; /// The individual method calls in the stack trace. private StackFrame[]? Frames; /// The string representation of the stack trace. private string? StringForm; /********* ** Public methods *********/ /// Construct an instance. /// The underlying stack trace. public ImmutableStackTrace(StackTrace stackTrace) { this.StackTrace = stackTrace; } /// Get the underlying frames. /// This is a reference to the underlying stack frames, so this array should not be edited. public StackFrame[] GetFrames() { return this.Frames ??= this.StackTrace.GetFrames(); } /// public override string ToString() { return this.StringForm ??= this.StackTrace.ToString(); } /// Get the current stack trace. /// The number of frames up the stack from which to start the trace. public static ImmutableStackTrace Get(int skipFrames = 0) { return new ImmutableStackTrace( new StackTrace(skipFrames: skipFrames + 1) // also skip this method ); } } }