Recipe13.15.Accessing Local Variable Information


Recipe 13.15. Accessing Local Variable Information

Problem

You are building a tool that examines code and you need to get access to the local variables within a method.

Solution

Use the LocalVariables property on the MethodBody class to return an IList of LocalVariableInfo objects, each of which describes a local variable within the method:

 public static IList<LocalVariableInfo> GetLocalVars(string asmPath,                                     string typeName, string methodName) {     Assembly asm = Assembly.LoadFrom(asmPath);     Type asmType = asm.GetType(typeName);     MethodInfo mi = asmType.GetMethod(methodName);     MethodBody mb = mi.GetMethodBody();     IList<LocalVariableInfo> vars = mb.LocalVariables;     // Display information about each local variable.     foreach (LocalVariableInfo lvi in vars)     {         Console.WriteLine("IsPinned: " + lvi.IsPinned);         Console.WriteLine("LocalIndex: " + lvi.LocalIndex);         Console.WriteLine("LocalType.Module: " + lvi.LocalType.Module);         Console.WriteLine("LocalType.FullName: " + lvi.LocalType.FullName);         Console.WriteLine("ToString(): " + lvi.ToString());     }     return (vars); } 

The GetLocalVars method can be called using the following code:

 public static void TestGetLocalVars() {     Process current = Process.GetCurrentProcess();     // Get the path of the current module.     string path = current.MainModule.FileName;     // Get all local var info for the CSharpRecipes.Reflection.GetLocalVars method.     System.Collections.ObjectModel.ReadOnlyCollection<LocalVariableInfo> vars =         (System.Collections.ObjectModel.ReadOnlyCollection<LocalVariableInfo>)         GetLocalVars(path, "CSharpRecipes.Reflection", "GetLocalVars"); } 

The output of this method is shown here:

 IsPinned: False LocalIndex: 0 LocalType.Module: CommonLanguageRuntimeLibrary LocalType.FullName: System.Reflection.Assembly ToString( ): System.Reflection.Assembly (0) IsPinned: False LocalIndex: 1 LocalType.Module: CommonLanguageRuntimeLibrary LocalType.FullName: System.Type ToString( ): System.Type (1) IsPinned: False LocalIndex: 2 LocalType.Module: CommonLanguageRuntimeLibrary LocalType.FullName: System.Reflection.MethodInfo ToString( ): System.Reflection.MethodInfo (2) IsPinned: False LocalIndex: 3 LocalType.Module: CommonLanguageRuntimeLibrary LocalType.FullName: System.Reflection.MethodBody ToString( ): System.Reflection.MethodBody (3) IsPinned: False LocalIndex: 4 LocalType.Module: CommonLanguageRuntimeLibrary LocalType.FullName: System.Collections.ObjectModel.ReadOnlyCollection`1[[System. Reflection.LocalVariableInfo, mscorlib, Version=2.0.0.0, Culture=neutral, Public KeyToken=b77a5c561934e089]] ToString( ): System.Collections.ObjectModel.ReadOnlyCollection`1[System.Reflectio n.LocalVariableInfo] (4) 

The LocalVariableInfo objects for each local variable found in the CSharpRecipes. Reflection.GetLocalVars method will be returned in the vars IList collection.

Discussion

The LocalVariables property can give you a good amount of information about variables within a method. The LocalVariables property returns an IList<LocalVariableInfo> collection. Each LocalVariableInfo object contains the information described in Table 13-5.

Table 13-5. LocalVariableInfo information

Member

Definition

IsPinned

Returns a bool indicating if the object that this variable refers to is pinned in memory (TRue) or not (false)

LocalIndex

Returns the index of this variable within this method's body

LocalType

Returns a Type object that describes the type of this variable

ToString

Returns the LocalType.FullName, a space, and then the LocalIndex value surrounded by parentheses


See Also

See the "MethodInfo Class," "MethodBody Class," "ReadOnlyCollection<T> Class," and "LocalVariableInfo Class" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net