Export (0) Print
Expand All

LocalBuilder Class

Represents a local variable within a method or constructor.

For a list of all members of this type, see LocalBuilder Members.

System.Object
   System.Reflection.Emit.LocalBuilder

[Visual Basic]
NotInheritable Public Class LocalBuilder
[C#]
public sealed class LocalBuilder
[C++]
public __gc __sealed class LocalBuilder
[JScript]
public class LocalBuilder

Thread Safety

Reflection Emit is thread-safe when using assemblies that were created with the AppDomain.DefineDynamicAssembly method with the Boolean parameter isSynchronized set to true.

Remarks

A LocalBuilder object can be defined using the DeclareLocal method.

Example

[Visual Basic, C#, C++] The following example demonstrates the use of LocalBuilder.

[Visual Basic] 
Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Threading

Class LocalBuilder_Sample

   Public Shared Sub Main()
      Try

         ' 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), _
                                                                        FieldAttributes.Private)

         ' 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()
         myConstructorIL.Emit(OpCodes.Ldarg_0)
         Dim mySuperConstructor As ConstructorInfo = GetType(Object).GetConstructor(New Type() {})
         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.
         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))
         myLB1.SetLocalSymInfo("myString")
         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.Stloc_1)
         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

[C#] 
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;

class LocalBuilder_Sample
{
   public static void Main()
   {
      try
      {

         // Create an assembly.
         AssemblyName myAssemblyName = new 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", typeof(String), FieldAttributes.Private);

         // Create the constructor.
         Type[] myConstructorArgs = { typeof(String) };
         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 = typeof(Object).GetConstructor(new 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, typeof(String), null);

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

         // Create local variables.
         LocalBuilder myLB1 = myMethodIL.DeclareLocal(typeof(string));
         myLB1.SetLocalSymInfo("myString");
         Console.WriteLine("'myLB1' type is :{0}", myLB1.LocalType);
         LocalBuilder myLB2 = myMethodIL.DeclareLocal(typeof(int));
         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'.
         Object myObject1 = Activator.CreateInstance(myType1, new Object[] { "HelloWorld" });

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

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Threading;

int main()
{
   try
   {

      // Create an assembly.
      AssemblyName* myAssemblyName = new AssemblyName();
      myAssemblyName->Name = S"SampleAssembly";

      AssemblyBuilder* myAssembly = Thread::GetDomain()->DefineDynamicAssembly(
         myAssemblyName, AssemblyBuilderAccess::Run);

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

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

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

      // Create the constructor.
      Type* myConstructorArgs[] = { __typeof(String) };
      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 = __typeof(Object)->GetConstructor(new 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(S"Function1",
         MethodAttributes::Public, __typeof(String), 0);

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

      // Create local variables.
      LocalBuilder* myLB1 = myMethodIL->DeclareLocal(__typeof(String));
      myLB1->SetLocalSymInfo(S"myString");
      Console::WriteLine(S"'myLB1' type is :{0}", myLB1->LocalType);
      LocalBuilder* myLB2 = myMethodIL->DeclareLocal(__typeof(int));
      myLB2->SetLocalSymInfo(S"myInt",1,2);
      Console::WriteLine(S"'myLB2' type is :{0}", myLB2->LocalType);
      myMethodIL->Emit(OpCodes::Ldstr, S"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(S"'myClass' is created.");
      // Create an instance of the 'myClass'.

      Object* temp0 [] = {S"HelloWorld"};
      Object* myObject1 = Activator::CreateInstance(myType1, temp0);

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Reflection.Emit

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

LocalBuilder Members | System.Reflection.Emit Namespace

Show:
© 2014 Microsoft