C# - need to know when variable goes out of scope
<p>I have some C++ code that I'm trying to port to C#.  The original author had a cool indented debugging C++ class that works like this:</p>

<pre><code>void a()
{
   indented_debug id;
   id.trace("I'm in A");
}
void b()
{
   indented_debug id;
   id.trace("I'm in B");
   a();
}
void e()
{
   a();
}
void d()
{
   e();
}
void c()
{
   indented_debug id;
   id.trace("I'm in C");
   a();
   b();
   {
      indented_debug id2;
      id2.trace("I'm still in C");
      d();
   }
}
</code></pre>

<p>And what you see in the output is this:</p>

<pre><code>I'm in C
   I'm in A
   I'm in B
      I'm in A
   I'm still in C
      I'm in A
</code></pre>

<p>Which makes it really easy to see not only the order in which functions are being called, but who's calling whom.  The indentation (which is the key thing here) is automatically handled by the construction &amp; destruction of the "indented_debug" objects.  Every time an "indented_debug" object is constructed, it increments a "how much shall I indent" counter; every time an "indented_debug" object is destructed, it decrements that counter.  It's this automatic calculation of the indentation that is the key to this class.</p>

<p>Of course, C# doesn't like this at all.  C# goes out of its way to make sure that you are absolutely completely prevented from knowing when a variable has gone out of scope.  And yes, I know how garbage collection works, and I do like it, but it seems like Microsoft could have given us a function IsThisObjectUnreachable() or something like that.  Or, an attribute keyword [RefCount] that means "Do reference counting on this object instead of garbage collection".</p>

<p>I can't find any way to know enough about an object to know whether it's gone out of scope, is there some clever way to provide this same functionality in C#?</p>

<p>I should also throw in this design restriction: I would really rather not wrap all of my functions in "using (indented_debug id = new id) { }", the idea is to have this debugging capability with as little impact on the code and its readability is possible.</p>

<p>[Added later]</p>

<p>It's a little tricky, adding to the original question later like this, but I need to write some code and can't do that in comments.</p>

<p>The StackTrace method is oh-so-close to the solution I was looking for, let me explain what it looks like.</p>

<pre><code>public class indented_debug
{
   static int minFrame = 999;
   static void trace(string text)
   {
        StackTrace stackTrace = new StackTrace();
        StackFrame[] frames = stackTrace.GetFrames();

        if (frames.Length &lt; minFrame)
            minFrame = frames.Length;

        String indent = new String(' ', (frames.Length - minFrame) * 3);
        Debug.WriteLine(indent + text);
   }
}
</code></pre>

<p>This is extra cool because you don't even need to construct an object of type indented_debug - the indentation is completely controlled by how deep in the stack you are.  The drawback, of course, is that in my example when c() calls d(), there are two extra stack frames in there where there is no tracing happening, so the indentation would be more than is required.  Rob has suggested a way around this by adding a custom attribute to the methods, which does solve that problem (I didn't include his code in my example, you can read it below).</p>

<p>But there is another issue, the StackTrace concept doesn't allow for additional indentation inside a function (like I have in my original c() function).  I was thinking that the number of times where the code has extra indents inside a function is quite small, so it's probably acceptable to add the "using" block in those cases.  That means the C# code looks like this:</p>

<pre><code>[IndentLog]
void a()
{
   indented_debug.trace("I'm in A");
}
[IndentLog]
void b()
{
   indented_debug.trace("I'm in B");
   a();
}
void e()
{
   a();
}
void d()
{
   e();
}
[IndentLog]
void c()
{
   indented_debug.trace("I'm in C");
   a();
   b();
   using (indented_debug id = new indented_debug())
   {
      indented_debug.trace("I'm still in C");
      d();
   }
}
</code></pre>

<p>And then the object 'id' is constructed &amp; finalized in a deterministic way, and I can create a data structure where I associate 'id' with the current stack frame when it constructs, and de-associate it when it finalizes.</p>