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)

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

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.

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;

class LocalBuilder_Sample
    public static void Main()
        // Create an assembly.
        AssemblyName myAssemblyName = new AssemblyName();
        myAssemblyName.Name = "SampleAssembly";

        AssemblyBuilder myAssembly = 

        // Create a module. For a single-file assembly the module
        // name is usually the same as the assembly name.
        ModuleBuilder myModule = 
                myAssemblyName.Name + ".dll", true);

        // Define a public class 'Example'.
        TypeBuilder myTypeBuilder = 
            myModule.DefineType("Example", TypeAttributes.Public);

        // Create the 'Function1' public method, which takes an integer
        // and returns a string.
        MethodBuilder myMethod = myTypeBuilder.DefineMethod("Function1",
           MethodAttributes.Public | MethodAttributes.Static, 
           typeof(String), new Type[] { typeof(int) });

        // 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.
        ILGenerator myMethodIL = myMethod.GetILGenerator();

        // Create local variables named myString and myInt.
        LocalBuilder myLB1 = myMethodIL.DeclareLocal(typeof(string));
        Console.WriteLine("local 'myString' type is: {0}", myLB1.LocalType);

        LocalBuilder myLB2 = myMethodIL.DeclareLocal(typeof(int));
        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.
        Type myType1 = 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.
        Object myObject2 = myType1.InvokeMember("Function1", 
            BindingFlags.InvokeMethod, null, null, new Object[] { 42 });

        Console.WriteLine("Example.Function1 returned: {0}", myObject2);
/* 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

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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft. All rights reserved.