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
설명
LocalBuilder 개체는 DeclareLocal 메서드를 사용하여 정의할 수 있습니다.
예제
다음 예제에서는 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
스레드로부터의 안전성
이 형식의 모든 public static(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에서 지원