LocalBuilder Class
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Represents a local variable within a method or constructor.
Inheritance Hierarchy
System.Object
System.Reflection.LocalVariableInfo
System.Reflection.Emit.LocalBuilder
Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public NotInheritable Class LocalBuilder _
Inherits LocalVariableInfo
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public sealed class LocalBuilder : LocalVariableInfo
The LocalBuilder type exposes the following members.
Properties
Name | Description | |
---|---|---|
IsPinned | Gets a value indicating whether the object referred to by the local variable is pinned in memory. (Overrides LocalVariableInfo.IsPinned.) | |
LocalIndex | Gets the zero-based index of the local variable within the method body. (Overrides LocalVariableInfo.LocalIndex.) | |
LocalType | Gets the type of the local variable. (Overrides LocalVariableInfo.LocalType.) |
Top
Methods
Name | Description | |
---|---|---|
Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
SetLocalSymInfo(String) | Sets the name of this local variable. | |
SetLocalSymInfo(String, Int32, Int32) | Sets the name and lexical scope of this local variable. | |
ToString | Returns a user-readable string that describes the local variable. (Inherited from LocalVariableInfo.) |
Top
Examples
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. 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.
Note: |
---|
To run this example, see Building Examples That Use a Demo Method and a TextBlock Control. |
Imports System.Reflection
Imports System.Reflection.Emit
Class Example
Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
' Create an assembly.
Dim myAssemblyName As New AssemblyName("SampleAssembly")
Dim myAssembly As AssemblyBuilder = _
AppDomain.CurrentDomain.DefineDynamicAssembly(myAssemblyName, _
AssemblyBuilderAccess.Run)
' Create a module. For a single-file assembly the module
' name is usually the same as the assembly name.
Dim myModule As ModuleBuilder = _
myAssembly.DefineDynamicModule(myAssemblyName.Name)
' Define a public class 'MyExample'.
Dim myTypeBuilder As TypeBuilder = _
myModule.DefineType("MyExample", TypeAttributes.Public)
' Create the 'Function1' public method, which takes an Integer
' and returns a string.
Dim myMethod As MethodBuilder = myTypeBuilder.DefineMethod("Function1", _
MethodAttributes.Public Or MethodAttributes.Static, _
GetType(String), New Type() {GetType(Integer)})
' 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.
Dim myMethodIL As ILGenerator = myMethod.GetILGenerator()
' Create local variables named myString and myInt.
Dim myLB1 As LocalBuilder = myMethodIL.DeclareLocal(GetType(String))
outputBlock.Text &= String.Format("local 'myString' type is: {0}", myLB1.LocalType) & vbCrLf
Dim myLB2 As LocalBuilder = myMethodIL.DeclareLocal(GetType(Integer))
outputBlock.Text &= String.Format("local 'myInt' type is: {0}", myLB2.LocalType) & vbCrLf
' 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.
Dim myType1 As Type = myTypeBuilder.CreateType()
' Invoke 'Function1' method of 'MyExample', passing the value 42.
Dim myObject2 As Object = myType1.InvokeMember("Function1", _
BindingFlags.InvokeMethod, Nothing, Nothing, New Object() {42})
outputBlock.Text &= String.Format("Example.Function1 returned: {0}", myObject2) & vbCrLf
End Sub
End Class
' This code example produces the following output:
'
'local 'myString' type is: System.String
'local 'myInt' type is: System.Int32
'Example.Function1 returned: string value
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
// Create an assembly.
AssemblyName myAssemblyName = new AssemblyName("SampleAssembly");
AssemblyBuilder myAssembly =
AppDomain.CurrentDomain.DefineDynamicAssembly(myAssemblyName,
AssemblyBuilderAccess.Run);
// Create a module. For a single-file assembly the module
// name is usually the same as the assembly name.
ModuleBuilder myModule =
myAssembly.DefineDynamicModule(myAssemblyName.Name);
// 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));
outputBlock.Text += String.Format("local 'myString' type is: {0}\n", myLB1.LocalType);
LocalBuilder myLB2 = myMethodIL.DeclareLocal(typeof(int));
outputBlock.Text += String.Format("local 'myInt' type is: {0}\n", 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();
// Invoke 'Function1' method of 'Example', passing the value 42.
Object myObject2 = myType1.InvokeMember("Function1",
BindingFlags.InvokeMethod, null, null, new Object[] { 42 });
outputBlock.Text += String.Format("Example.Function1 returned: {0}\n", myObject2);
}
}
/* This code example produces the following output:
local 'myString' type is: System.String
local 'myInt' type is: System.Int32
Example.Function1 returned: string value
*/
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.