Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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 sealed class LocalBuilder : LocalVariableInfo, _LocalBuilder
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
public final class LocalBuilder extends LocalVariableInfo implements _LocalBuilder
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.None) 
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 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 );
      }
   }
}

System.Object
   System.Reflection.LocalVariableInfo
    System.Reflection.Emit.LocalBuilder
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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.