This documentation is archived and is not being maintained.

LocalBuilder Class

Represents a local variable within a method or constructor.

Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)

<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public NotInheritable Class LocalBuilder
	Inherits LocalVariableInfo
	Implements _LocalBuilder
Dim instance As LocalBuilder

/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
public final class LocalBuilder extends LocalVariableInfo implements _LocalBuilder
public final class LocalBuilder extends LocalVariableInfo implements _LocalBuilder

A LocalBuilder object can be defined using the DeclareLocal method.

The following example demonstrates the use of LocalBuilder.

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.Run)

         ' Create a module 'myModule'.
         Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("SampleModule", True)

         ' Define a public class 'myClass'.
         Dim myTypeBuilder As TypeBuilder = myModule.DefineType("myClass", TypeAttributes.Public)

         ' Define a private String field.
         Dim myField As FieldBuilder = myTypeBuilder.DefineField("myMessage", GetType(String), _

         ' Create the constructor.
         Dim myConstructorArgs As Type() = {GetType(String)}
         Dim myConstructor As ConstructorBuilder = myTypeBuilder.DefineConstructor _
                           (MethodAttributes.Public, CallingConventions.Standard, myConstructorArgs)

         ' Generate IL for the method.
         Dim myConstructorIL As ILGenerator = myConstructor.GetILGenerator()
         Dim mySuperConstructor As ConstructorInfo = GetType(Object).GetConstructor(New Type() {})
         myConstructorIL.Emit(OpCodes.Call, mySuperConstructor)
         myConstructorIL.Emit(OpCodes.Stfld, myField)

         ' Create the 'Function1' public method.
         Dim myMethod As MethodBuilder = myTypeBuilder.DefineMethod("Function1", MethodAttributes. _
                                                   Public, GetType(String), Nothing)

         ' Generate IL for 'Function1'.
         Dim myMethodIL As ILGenerator = myMethod.GetILGenerator()

         ' Create local variables.
         Dim myLB1 As LocalBuilder = myMethodIL.DeclareLocal(GetType(String))
         Console.WriteLine("'myLB1' type is :{0}", myLB1.LocalType)
         Dim myLB2 As LocalBuilder = myMethodIL.DeclareLocal(GetType(Integer))
         myLB2.SetLocalSymInfo("myInt", 1, 2)
         Console.WriteLine("'myLB2' type is :{0}", myLB2.LocalType)

         myMethodIL.Emit(OpCodes.Ldstr, "Local value"  )
         myMethodIL.Emit(OpCodes.Stloc_0 )
         myMethodIL.Emit(OpCodes.Ldloc_0 )
         myMethodIL.Emit(OpCodes.Ldloc_1 )
         myMethodIL.Emit(OpCodes.Ret )

         ' Create "myClass" class.
         Dim myType1 As Type = myTypeBuilder.CreateType()

         Console.WriteLine("'myClass' is created.")
         ' Create an instance of the 'myClass'.
         Dim myObject1 As Object = Activator.CreateInstance(myType1, New Object() {"HelloWorld"})

         ' Invoke 'Function1' method of 'myClass'.
         Dim myObject2 As Object = myType1.InvokeMember("Function1", BindingFlags.InvokeMethod, _
                                                                     Nothing, myObject1, Nothing)
         Console.WriteLine("myClass.Function1 is called.")
         Console.WriteLine("myClass.Function1 returned: {0}", myObject2)
      Catch e As Exception
         Console.WriteLine("Exception :{0}", e.Message)
      End Try
   End Sub 'Main
End Class 'LocalBuilder_Sample


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0