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)

public ref class LocalBuilder sealed : public LocalVariableInfo, _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.

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Threading;
int main()
      // Create an assembly.
      AssemblyName^ myAssemblyName = gcnew AssemblyName;
      myAssemblyName->Name = "SampleAssembly";
      AssemblyBuilder^ myAssembly = Thread::GetDomain()->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::Run );

      // Create a module 'myModule'.
      ModuleBuilder^ myModule = myAssembly->DefineDynamicModule( "SampleModule", true );

      // Define a public class 'myClass'.
      TypeBuilder^ myTypeBuilder = myModule->DefineType( "myClass", TypeAttributes::Public );

      // Define a private String field.
      FieldBuilder^ myField = myTypeBuilder->DefineField( "myMessage", String::typeid, FieldAttributes::Private );

      // Create the constructor.
      array<Type^>^myConstructorArgs = {String::typeid};
      ConstructorBuilder^ myConstructor = myTypeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, myConstructorArgs );

      // Generate IL for the method.
      ILGenerator^ myConstructorIL = myConstructor->GetILGenerator();
      myConstructorIL->Emit( OpCodes::Ldarg_0 );
      ConstructorInfo^ mySuperConstructor = Object::typeid->GetConstructor( gcnew array<Type^>(0) );
      myConstructorIL->Emit( OpCodes::Call, mySuperConstructor );
      myConstructorIL->Emit( OpCodes::Ldarg_0 );
      myConstructorIL->Emit( OpCodes::Ldarg_1 );
      myConstructorIL->Emit( OpCodes::Stfld, myField );
      myConstructorIL->Emit( OpCodes::Ret );

      // Create the 'Function1' public method.
      MethodBuilder^ myMethod = myTypeBuilder->DefineMethod( "Function1", MethodAttributes::Public, String::typeid, nullptr );

      // Generate IL for 'Function1'.
      ILGenerator^ myMethodIL = myMethod->GetILGenerator();

      // Create local variables.
      LocalBuilder^ myLB1 = myMethodIL->DeclareLocal( String::typeid );
      myLB1->SetLocalSymInfo( "myString" );
      Console::WriteLine( "'myLB1' type is :{0}", myLB1->LocalType );
      LocalBuilder^ myLB2 = myMethodIL->DeclareLocal( int::typeid );
      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::Stloc_1 );
      myMethodIL->Emit( OpCodes::Ldloc_1 );
      myMethodIL->Emit( OpCodes::Ret );

      // Create "myClass" class.
      Type^ myType1 = myTypeBuilder->CreateType();
      Console::WriteLine( "'myClass' is created." );

      // Create an instance of the 'myClass'.
      array<Object^>^temp0 = {"HelloWorld"};
      Object^ myObject1 = Activator::CreateInstance( myType1, temp0 );

      // Invoke 'Function1' method of 'myClass'.
      Object^ myObject2 = myType1->InvokeMember( "Function1", BindingFlags::InvokeMethod, nullptr, myObject1, nullptr );
      Console::WriteLine( "myClass.Function1 is called." );
      Console::WriteLine( "myClass.Function1 returned: {0}", myObject2 );
   catch ( Exception^ e ) 
      Console::WriteLine( "Exception :{0}", e->Message );


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