Is there a way to enumerate current variables from within the C# itself?
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
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- Marked As Answer byKarel ZikmundMSFT, ModeratorFriday, October 23, 2009 10:19 PM
- Proposed As Answer byKarel ZikmundMSFT, ModeratorWednesday, October 21, 2009 7:01 PM
- 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- Marked As Answer byeryangMSFT, ModeratorTuesday, October 27, 2009 9:54 AM
All Replies
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- Marked As Answer byKarel ZikmundMSFT, ModeratorFriday, October 23, 2009 10:19 PM
- Proposed As Answer byKarel ZikmundMSFT, ModeratorWednesday, October 21, 2009 7:01 PM
- 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. - 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 - 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! 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- Proposed As Answer byKarel ZikmundMSFT, ModeratorFriday, October 23, 2009 10:20 PM
- I am familiar with Native API for MetaData and I use it in my code.
But how can I access metadata from C# itself? - 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- Marked As Answer byeryangMSFT, ModeratorTuesday, October 27, 2009 9:54 AM


