views:

48

answers:

2

Disclaimers:

  • I am not interested in doing this in any real production code.
  • I make no claim that there's a good reason why I'm interested in doing this.
  • I realize that if this is in any way possible, it must involve doing some highly inadvisable things.

That said... is this possible? I'm really just curious to know.

In other words, if I have something like this:

int i = GetSomeInteger();

Is there any way from within GetSomeInteger that the code could be "aware of" the fact that it's being called in an assignment to the variable i?

Again: no interest in doing this in any sort of real scenario. Just curious!

A: 

I don't know specifically how one would access it, but theoretically, if the code is compiled with debug symbols (like, in the debug configuration) then the information is there.

Wyatt
+2  A: 

It's possible using System.Diagnostics.StackTrace.

For instance, you can get the name of the calling method like so:

    private static void stackExample()
    {
        var stack = new System.Diagnostics.StackTrace(true); // pass true to get more stack info

        var callingMethod = stack.GetFrame(1).GetMethod().Name;
        var callingLine = stack.GetFrame(1).GetFileLineNumber();

        Console.WriteLine("callingMethod: " + callingMethod + " on line " + callingLine.ToString());
    }

http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx

This will not give you the line of code of the calling method, you'll have to have access to the source code for that. stack.GetFrame(1).GetFileName() will give you the filename of the source code containing the method. What you can do from here, with the Method info and line number is to open the source file and get the line of code in question.

GetMethod() gives you all sorts of great information, like which module the method exists in, then from there you can get the assembly information.

It's actually pretty fun to search through all of the metadata, it tells you all kinds of cool stuff about your code.

The reason you cannot get the actual line of code is the source itself in C# form is not stored in the assembly. While you can get the IL, it's a little more difficult to read. :)

Jeff Schumacher
That's a start, but he seems to want it down to the actual calling line.
Steven Sudit
This is helpful, and I can see how to get (1) the line *number* as well as (2) the calling *method* from this. But it's unclear to me how to put these together to get the actual line of code itself.
Dan Tao
@Dan, @Steven: I expanded my answer.
Jeff Schumacher
This seems to be pretty definitive to me, and is consistent with I've found from my own searching. Thanks!
Dan Tao
@Dan Tao, I know you don't intend to use that in production code, but be aware that if the code is optimized (it's usually the case when you build in Release configuration), the stack information is not reliable ; a method could have been inlined, in which case it won't appear in the stack trace
Thomas Levesque
@Thomas: Duly noted. To be honest, the sheer difficulty entailed by attempting this in the first place is pretty much prohibitive to me going much further with this little experiment of mine (which is probably a good thing anyway).
Dan Tao