Answered by:
How to get local variable names defined in a method using reflection in C#.net

Question
-
How to get local variable names defined inside a method using reflection(.net assembly).
I can get local variable info using MethodInfo.MethodBody().LocalVariables. But it gives data type of the local variables not the name.
So is there any way that i can get variable names.
Friday, May 10, 2013 9:55 AM
Answers
-
"But again how do i map this information with the information(LocalVariables defined in a method)"
With SymReader the locals can be found via ISymbolMethod.RootScope. Here's a short example:
var locals = typeof(TestClass).GetMethod("GetStringRepresentation").GetMethodBody().LocalVariables; foreach (var scope in met.RootScope.GetChildren()) { foreach (var loc in scope.GetLocals()) { Console.WriteLine("{0}: {1} {2}", loc.AddressField1, locals.FirstOrDefault(l => l.LocalIndex == loc.AddressField1).LocalType, loc.Name); } }
It's incomplete (should be recursive to be able to enumerate all scopes, may need some sanity checks like verifying if the AddressKind is ILOffset) but you should get the idea.- Proposed as answer by Mike Feng Tuesday, May 14, 2013 2:34 AM
- Marked as answer by RK Engineer Tuesday, May 14, 2013 5:29 AM
Friday, May 10, 2013 11:58 AM
All replies
-
The name of local variables isn't stored in metadata, you can't get this information from reflection. What are you trying to achieve?Friday, May 10, 2013 10:19 AM
-
I am creating a visual studio plugin to analyze code and accordingly display error in error list window.
So, I want to find specific type of variable in a method and then accordingly display error in error list window. But error should provide navigation to exact location in a method where that variable is defined. Using reflection i get the local variables defined in a method and i can check the type of the variables but i can't find the location in a method body where it is defined. Do you have any idea how can i achieve this.
I am refering http://www.mztools.com/articles/2008/MZ2008022.aspx link to display error in error list window.
Friday, May 10, 2013 10:39 AM -
"But error should provide navigation to exact location in a method where that variable is defined."
This can't be done only with reflection, the necessary information can only be obtained from the .pdb file produced during compilation.
I don't know how you analyze code, I assume you must have a IL reader of some sort. Some IL readers also can read .pdb files and should be able to provide the necessary information. For example CCI: http://ccimetadata.codeplex.com/
Friday, May 10, 2013 10:45 AM -
Yes Mike,
I am using IL Reader as specified at http://sorin.serbans.net/blog/?p=257.
That way i can get offset and line numbers. But again how do i map this information with the information(LocalVariables defined in a method) that i get using reflection so that i can get the line number where specific type of variable is defined.
Can Offset help me?
- Edited by RK Engineer Friday, May 10, 2013 11:54 AM more info
Friday, May 10, 2013 11:37 AM -
"But again how do i map this information with the information(LocalVariables defined in a method)"
With SymReader the locals can be found via ISymbolMethod.RootScope. Here's a short example:
var locals = typeof(TestClass).GetMethod("GetStringRepresentation").GetMethodBody().LocalVariables; foreach (var scope in met.RootScope.GetChildren()) { foreach (var loc in scope.GetLocals()) { Console.WriteLine("{0}: {1} {2}", loc.AddressField1, locals.FirstOrDefault(l => l.LocalIndex == loc.AddressField1).LocalType, loc.Name); } }
It's incomplete (should be recursive to be able to enumerate all scopes, may need some sanity checks like verifying if the AddressKind is ILOffset) but you should get the idea.- Proposed as answer by Mike Feng Tuesday, May 14, 2013 2:34 AM
- Marked as answer by RK Engineer Tuesday, May 14, 2013 5:29 AM
Friday, May 10, 2013 11:58 AM -
Hello,Can you tell me how to get SymReader?
i copy your example to my vs,but met is not defined...
Wednesday, May 30, 2018 9:16 AM -
You get the ISymReader from an ISymBinder1, apparently there is an implementation in the ISymWrapper assembly, but I have always used the COM api instead.Thursday, June 7, 2018 10:00 PM