LocalBuilder 类
表示方法或构造函数内的局部变量。
**命名空间:**System.Reflection.Emit
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public NotInheritable Class LocalBuilder
Inherits LocalVariableInfo
Implements _LocalBuilder
用法
Dim instance As LocalBuilder
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public sealed class LocalBuilder : LocalVariableInfo, _LocalBuilder
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType::None)]
public ref class LocalBuilder sealed : public 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
备注
可以使用 DeclareLocal 方法定义 LocalBuilder 对象。
示例
下面的示例演示了 LocalBuilder 的用法。
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
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 );
}
}
}
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Threading;
int main()
{
try
{
// 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 );
}
}
继承层次结构
System.Object
System.Reflection.LocalVariableInfo
System.Reflection.Emit.LocalBuilder
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
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
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0