.NET Framework Developer Center > .NET Development Forums > Building Development and Diagnostic Tools for .Net > Is there a way to enumerate current variables from within the C# itself?
Ask a questionAsk a question
 

AnswerIs there a way to enumerate current variables from within the C# itself?

  • Wednesday, October 21, 2009 4:57 AMlpszDan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    All those System.Reflection and metadata apis included in .NET made me wonder,
    is it possible for a program code to just enumerate or get access from within itself to a list of all
    local variables or to a hierarchy of scopes with variables up to the global one?

Answers

All Replies

  • Wednesday, October 21, 2009 7:01 PMKarel ZikmundMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    The program can Reflect itself, check out Reflection APIs in System.Reflection namespace.
    You can get current method: MethodInfo.GetCurrentMethod().
    Local variables list: MethodBody.LocalVariables.
    And stack trace: System.Diagnostics.StackTrace.
    I bet that you can get to the global variables for each module somehow as well.

    Check out this article which uses some of these APIs: http://www.codeproject.com/KB/trace/customtracelistener.aspx

    -Karel

  • Friday, October 23, 2009 8:08 AMlpszDan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    That's very interesting. To my surprise it actually works! :-) I could get the stack frames. Amazing.
    But how do I get the value and more info of each localvariable? All I get is an index.
    Thanks.
  • Friday, October 23, 2009 3:24 PMKarel ZikmundMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I don't think you can get the values of local variables. What if some variables are optimized out? For inspection of local variable values, you will have to use native debugging APIs. However note that an application cannot debug itself, you will need external debugger app for that.

    The best you can get about local variables is LocalVariableInfo - see MethodBody.LocalVariables. Also note that you will not get the name of local variables, as they are not stored in the image. I think you could get the names via debugging APIs (from PDBs).

    Out of curiostity: Why do you need all this info? Are you working on some extensive logging mechanism, crash info collection or a debugger?

    -Karel
  • Friday, October 23, 2009 9:14 PMlpszDan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    No, what I am working on is a .NET debugger. Essentially a remote debugger. Check my site: www.remotedebugger.com. 

    So I was lucky that you guys now support .net much better than active scripting at the time. Without it the .net doc is a maze. There is so much info.

    Thanks by the way for the explanation on LocalVariables. There still must be a use of the index of local var.

    And I want to know how to access metadata from C# Could you help me with that?
    Thanks!
  • Friday, October 23, 2009 10:19 PMKarel ZikmundMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer

    To read MetaData you can use either Reflection, or unmanaged MetaData APIs (http://msdn.microsoft.com/en-us/library/ms404384.aspx). There is unfortunately not a good example for unmanaged MetaData API usage, tutorial or anything linke that on MSDN that I know of. You will have to search the internet to get more info and examples if you need it.

    There are also debugging APIs (check out sticky post in this forum for more links to mdbg and native debugging APIs). Mike Stall's blog could be useful too.

    The LocalIndex in LocalVariableInfo serves IMO as identifier. Read the MSDN page carefully - the note in LocalVariableInfo class says: "Local variable names are not persisted in metadata. In Microsoft intermediate language (MSIL), local variables are accessed by their position in the local variable signature."

    I agree that MSDN has a lot of information, however it is pretty good reference. For tutorials there are books and there are also great resources and examples on various web sites - just use your favourite search engine ...
    For example for LocalIndex you will find this:
        http://codeidol.com/csharp/csharpckbk2/Reflection/Accessing-Local-Variable-Information/

    -Karel

  • Sunday, October 25, 2009 8:02 AMlpszDan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I am familiar with Native API for MetaData and I use it in my code.
    But how can I access metadata from C# itself?
  • Monday, October 26, 2009 3:26 PMKarel ZikmundMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    From C# you can use either Reflection, native MetaData APIs (use PInvoke and COM interop) or some other external libraries (e.g. CCI - http://ccimetadata.codeplex.com/).

    -Karel