LocalBuilder Class
Represents a local variable within a method or constructor.
Assembly: mscorlib (in mscorlib.dll)
| Name | Description | |
|---|---|---|
![]() | IsPinned | Gets a value indicating whether the object referred to by the local variable is pinned in memory.(Overrides LocalVariableInfo.IsPinned.) |
![]() | LocalIndex | Gets the zero-based index of the local variable within the method body.(Overrides LocalVariableInfo.LocalIndex.) |
![]() | LocalType | Gets the type of the local variable.(Overrides LocalVariableInfo.LocalType.) |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified object is equal to the current object.(Inherited from Object.) |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() | GetType() | |
![]() | SetLocalSymInfo(String) | Sets the name of this local variable. |
![]() | SetLocalSymInfo(String, Int32, Int32) | Sets the name and lexical scope of this local variable. |
![]() | ToString() | Returns a user-readable string that describes the local variable.(Inherited from LocalVariableInfo.) |
| Name | Description | |
|---|---|---|
![]() ![]() | _LocalBuilder.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) | Maps a set of names to a corresponding set of dispatch identifiers. |
![]() ![]() | _LocalBuilder.GetTypeInfo(UInt32, UInt32, IntPtr) | Retrieves the type information for an object, which can then be used to get the type information for an interface. |
![]() ![]() | _LocalBuilder.GetTypeInfoCount(UInt32) | Retrieves the number of type information interfaces that an object provides (either 0 or 1). |
![]() ![]() | _LocalBuilder.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | Provides access to properties and methods exposed by an object. |
A LocalBuilder object can be defined using the DeclareLocal method.
The following example creates a static method (Shared in Visual Basic) named Function1 that returns a string and has a parameter of type Int32. In the body of the method, the code example creates LocalBuilder objects representing two local variables, and sets symbol information for the local variables. The method does not do anything significant, but the method body demonstrates storing a parameter to a local variable, storing a literal string to a local variable, and loading a local variable.
Imports System Imports System.Reflection Imports System.Reflection.Emit Imports System.Threading Class LocalBuilder_Sample Public Shared Sub Main() ' Create an assembly. Dim myAssemblyName As New AssemblyName() myAssemblyName.Name = "SampleAssembly" Dim myAssembly As AssemblyBuilder = _ Thread.GetDomain().DefineDynamicAssembly( myAssemblyName, _ AssemblyBuilderAccess.RunAndSave ) ' Create a module. For a single-file assembly the module ' name is usually the same as the assembly name. Dim myModule As ModuleBuilder = _ myAssembly.DefineDynamicModule(myAssemblyName.Name, _ myAssemblyName.Name & ".dll", True) ' Define a public class 'Example'. Dim myTypeBuilder As TypeBuilder = _ myModule.DefineType("Example", TypeAttributes.Public) ' Create the 'Function1' public method, which takes an Integer ' and returns a string. Dim myMethod As MethodBuilder = myTypeBuilder.DefineMethod("Function1", _ MethodAttributes.Public Or MethodAttributes.Static, _ GetType(String), New Type() { GetType(Integer) }) ' Generate IL for 'Function1'. The function body demonstrates ' assigning an argument to a local variable, assigning a ' constant string to a local variable, and putting the contents ' of local variables on the stack. Dim myMethodIL As ILGenerator = myMethod.GetILGenerator() ' Create local variables named myString and myInt. Dim myLB1 As LocalBuilder = myMethodIL.DeclareLocal(GetType(String)) myLB1.SetLocalSymInfo("myString") Console.WriteLine("local 'myString' type is: {0}", myLB1.LocalType) Dim myLB2 As LocalBuilder = myMethodIL.DeclareLocal(GetType(Integer)) myLB2.SetLocalSymInfo("myInt", 1, 2) Console.WriteLine("local 'myInt' type is: {0}", myLB2.LocalType) ' Store the function argument in myInt. myMethodIL.Emit(OpCodes.Ldarg_0 ) myMethodIL.Emit(OpCodes.Stloc_1 ) ' Store a literal value in myString, and return the value. myMethodIL.Emit(OpCodes.Ldstr, "string value" ) myMethodIL.Emit(OpCodes.Stloc_0 ) myMethodIL.Emit(OpCodes.Ldloc_0 ) myMethodIL.Emit(OpCodes.Ret ) ' Create "Example" class. Dim myType1 As Type = myTypeBuilder.CreateType() Console.WriteLine("'Example' is created.") myAssembly.Save(myAssemblyName.Name & ".dll") Console.WriteLine( "'{0}' is created.", myAssemblyName.Name & ".dll" ) ' Invoke 'Function1' method of 'Example', passing the value 42. Dim myObject2 As Object = myType1.InvokeMember("Function1", _ BindingFlags.InvokeMethod, Nothing, Nothing, New Object() { 42 }) Console.WriteLine("Example.Function1 returned: {0}", myObject2) End Sub End Class ' This code example produces the following output: ' 'local 'myString' type is: System.String 'local 'myInt' type is: System.Int32 ''Example' is created. ''SampleAssembly.dll' is created. 'Example.Function1 returned: string value
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.1
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.



