다음을 통해 공유


ParameterBuilder 클래스

정의

매개 변수 정보를 만들거나 연결합니다.

public ref class ParameterBuilder
public ref class ParameterBuilder abstract
public ref class ParameterBuilder : System::Runtime::InteropServices::_ParameterBuilder
public class ParameterBuilder
public abstract class ParameterBuilder
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public class ParameterBuilder : System.Runtime.InteropServices._ParameterBuilder
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public class ParameterBuilder : System.Runtime.InteropServices._ParameterBuilder
type ParameterBuilder = class
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type ParameterBuilder = class
    interface _ParameterBuilder
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ParameterBuilder = class
    interface _ParameterBuilder
Public Class ParameterBuilder
Public MustInherit Class ParameterBuilder
Public Class ParameterBuilder
Implements _ParameterBuilder
상속
ParameterBuilder
특성
구현

예제

다음 예제에서는 를 사용하여 참조로 전달된 매개 변수를 사용하여 ParameterBuilder동적 메서드를 만드는 방법을 보여 줍니다.

using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
Type^ BuildCustomerDataType()
{
   AppDomain^ myDomain = Thread::GetDomain();
   AssemblyName^ myAsmName = gcnew AssemblyName;
   myAsmName->Name = "MyDynamicAssembly";
   AssemblyBuilder^ myAsmBuilder = myDomain->DefineDynamicAssembly( myAsmName, AssemblyBuilderAccess::Run );
   ModuleBuilder^ myModBuilder = myAsmBuilder->DefineDynamicModule( "MyMod" );
   TypeBuilder^ myTypeBuilder = myModBuilder->DefineType( "CustomerData", TypeAttributes::Public );
   FieldBuilder^ customerNameBldr = myTypeBuilder->DefineField( "customerName", String::typeid, FieldAttributes::Private );
   FieldBuilder^ acctIDBldr = myTypeBuilder->DefineField( "acctID", String::typeid, FieldAttributes::Private );
   FieldBuilder^ balanceAmtBldr = myTypeBuilder->DefineField( "balanceAmt", double::typeid, FieldAttributes::Private );
   array<Type^>^temp0 = {String::typeid,String::typeid,double::typeid};
   ConstructorBuilder^ myCtorBuilder = myTypeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::HasThis, temp0 );
   ILGenerator^ ctorIL = myCtorBuilder->GetILGenerator();
   Type^ objType = Type::GetType( "System.Object" );
   ConstructorInfo^ objCtor = objType->GetConstructor( gcnew array<Type^>(0) );
   ctorIL->Emit( OpCodes::Ldarg_0 );
   ctorIL->Emit( OpCodes::Call, objCtor );
   ctorIL->Emit( OpCodes::Ldarg_0 );
   ctorIL->Emit( OpCodes::Ldarg_1 );
   ctorIL->Emit( OpCodes::Stfld, customerNameBldr );
   ctorIL->Emit( OpCodes::Ldarg_0 );
   ctorIL->Emit( OpCodes::Ldarg_2 );
   ctorIL->Emit( OpCodes::Stfld, acctIDBldr );
   ctorIL->Emit( OpCodes::Ldarg_0 );
   ctorIL->Emit( OpCodes::Ldarg_3 );
   ctorIL->Emit( OpCodes::Stfld, balanceAmtBldr );
   ctorIL->Emit( OpCodes::Ret );
   
   // This method will take an amount from a static pool and add it to the balance.
   // Note that we are passing the first parameter, fundsPool, by reference. Therefore,
   // we need to inform the MethodBuilder to expect a ref, by declaring the first
   // parameter's type to be System::Double& (a reference to a double).
   array<Type^>^temp4 = {Type::GetType( "System.Double&" ),double::typeid};
   MethodBuilder^ myMthdBuilder = myTypeBuilder->DefineMethod( "AddFundsFromPool", MethodAttributes::Public, double::typeid, temp4 );
   ParameterBuilder^ poolRefBuilder = myMthdBuilder->DefineParameter( 1, ParameterAttributes::Out, "fundsPool" );
   ParameterBuilder^ amountFromPoolBuilder = myMthdBuilder->DefineParameter( 2, ParameterAttributes::In, "amountFromPool" );
   ILGenerator^ mthdIL = myMthdBuilder->GetILGenerator();
   mthdIL->Emit( OpCodes::Ldarg_1 );
   mthdIL->Emit( OpCodes::Ldarg_1 );
   mthdIL->Emit( OpCodes::Ldind_R8 );
   mthdIL->Emit( OpCodes::Ldarg_2 );
   mthdIL->Emit( OpCodes::Sub );
   mthdIL->Emit( OpCodes::Stind_R8 );
   mthdIL->Emit( OpCodes::Ldarg_0 );
   mthdIL->Emit( OpCodes::Ldarg_0 );
   mthdIL->Emit( OpCodes::Ldfld, balanceAmtBldr );
   mthdIL->Emit( OpCodes::Ldarg_2 );
   mthdIL->Emit( OpCodes::Add );
   mthdIL->Emit( OpCodes::Stfld, balanceAmtBldr );
   mthdIL->Emit( OpCodes::Ldarg_0 );
   mthdIL->Emit( OpCodes::Ldfld, balanceAmtBldr );
   mthdIL->Emit( OpCodes::Ret );
   return myTypeBuilder->CreateType();
}

int main()
{
   Type^ custType = nullptr;
   Object^ custObj = nullptr;
   array<Type^>^custArgTypes = {String::typeid,String::typeid,double::typeid};
   
   // Call the method to build our dynamic class.
   custType = BuildCustomerDataType();
   Console::WriteLine( "---" );
   ConstructorInfo^ myCustCtor = custType->GetConstructor( custArgTypes );
   double initialBalance = 100.00;
   array<Object^>^temp5 = {"Joe Consumer","5678-XYZ",initialBalance};
   custObj = myCustCtor->Invoke( temp5 );
   array<MemberInfo^>^myMemberInfo = custType->GetMember( "AddFundsFromPool" );
   double thePool = 1000.00;
   Console::WriteLine( "The pool is currently ${0}", thePool );
   Console::WriteLine( "The original balance of the account instance is ${0}", initialBalance );
   double amountFromPool = 50.00;
   Console::WriteLine( "The amount to be subtracted from the pool and added to the account is ${0}", amountFromPool );
   Console::WriteLine( "---" );
   Console::WriteLine( "Calling {0} ...", myMemberInfo[ 0 ] );
   Console::WriteLine( "---" );
   array<Object^>^passMe = {thePool,amountFromPool};
   Console::WriteLine( "The new balance in the account instance is ${0}", custType->InvokeMember( "AddFundsFromPool", BindingFlags::InvokeMethod, nullptr, custObj, passMe ) );
   thePool = safe_cast<double>(passMe[ 0 ]);
   Console::WriteLine( "The new amount in the pool is ${0}", thePool );
}

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

class ParamBuilderDemo

{

   public static Type BuildCustomerDataType()
   {

    AppDomain myDomain = Thread.GetDomain();
    AssemblyName myAsmName = new AssemblyName();
    myAsmName.Name = "MyDynamicAssembly";

    AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(myAsmName,
                        AssemblyBuilderAccess.Run);

    ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule("MyMod");

    TypeBuilder myTypeBuilder = myModBuilder.DefineType("CustomerData",
                        TypeAttributes.Public);

    FieldBuilder customerNameBldr = myTypeBuilder.DefineField("customerName",
                            typeof(string),
                            FieldAttributes.Private);
    FieldBuilder acctIDBldr = myTypeBuilder.DefineField("acctID",
                            typeof(string),
                            FieldAttributes.Private);
    FieldBuilder balanceAmtBldr = myTypeBuilder.DefineField("balanceAmt",
                            typeof(double),
                            FieldAttributes.Private);
                                
    ConstructorBuilder myCtorBuilder = myTypeBuilder.DefineConstructor(
                            MethodAttributes.Public,
                            CallingConventions.HasThis,
                            new Type[] { typeof(string),
                                     typeof(string),
                                     typeof(double) });

    ILGenerator ctorIL = myCtorBuilder.GetILGenerator();

    Type objType = Type.GetType("System.Object");
        ConstructorInfo objCtor = objType.GetConstructor(new Type[] {});

    ctorIL.Emit(OpCodes.Ldarg_0);
    ctorIL.Emit(OpCodes.Call, objCtor);

    ctorIL.Emit(OpCodes.Ldarg_0);
    ctorIL.Emit(OpCodes.Ldarg_1);
    ctorIL.Emit(OpCodes.Stfld, customerNameBldr);

    ctorIL.Emit(OpCodes.Ldarg_0);
    ctorIL.Emit(OpCodes.Ldarg_2);
    ctorIL.Emit(OpCodes.Stfld, acctIDBldr);

    ctorIL.Emit(OpCodes.Ldarg_0);
    ctorIL.Emit(OpCodes.Ldarg_3);
    ctorIL.Emit(OpCodes.Stfld, balanceAmtBldr);

    ctorIL.Emit(OpCodes.Ret);

    // This method will take an amount from a static pool and add it to the balance.

    // Note that we are passing the first parameter, fundsPool, by reference. Therefore,
    // we need to inform the MethodBuilder to expect a ref, by declaring the first
    // parameter's type to be System.Double& (a reference to a double).

    MethodBuilder myMthdBuilder = myTypeBuilder.DefineMethod("AddFundsFromPool",
                        MethodAttributes.Public,
                        typeof(double),
                        new Type[] { Type.GetType("System.Double&"),
                                 typeof(double) });

    ParameterBuilder poolRefBuilder = myMthdBuilder.DefineParameter(1,
                        ParameterAttributes.Out,
                        "fundsPool");

    ParameterBuilder amountFromPoolBuilder = myMthdBuilder.DefineParameter(2,
                        ParameterAttributes.In,
                            "amountFromPool");

    ILGenerator mthdIL = myMthdBuilder.GetILGenerator();

    mthdIL.Emit(OpCodes.Ldarg_1);
    mthdIL.Emit(OpCodes.Ldarg_1);
    mthdIL.Emit(OpCodes.Ldind_R8);
    mthdIL.Emit(OpCodes.Ldarg_2);
    mthdIL.Emit(OpCodes.Sub);

    mthdIL.Emit(OpCodes.Stind_R8);

    mthdIL.Emit(OpCodes.Ldarg_0);
    mthdIL.Emit(OpCodes.Ldarg_0);
    mthdIL.Emit(OpCodes.Ldfld, balanceAmtBldr);
    mthdIL.Emit(OpCodes.Ldarg_2);
    mthdIL.Emit(OpCodes.Add);

    mthdIL.Emit(OpCodes.Stfld, balanceAmtBldr);

    mthdIL.Emit(OpCodes.Ldarg_0);
    mthdIL.Emit(OpCodes.Ldfld, balanceAmtBldr);
    mthdIL.Emit(OpCodes.Ret);

    return myTypeBuilder.CreateType();
   }

   public static void Main()
   {
    Type custType = null;
    object custObj = null;

    Type[] custArgTypes = new Type[] {typeof(string), typeof(string), typeof(double)};
    
    // Call the method to build our dynamic class.

    custType = BuildCustomerDataType();

    Console.WriteLine("---");

    ConstructorInfo myCustCtor = custType.GetConstructor(custArgTypes);
    double initialBalance = 100.00;
    custObj = myCustCtor.Invoke(new object[] { "Joe Consumer",
                           "5678-XYZ",
                           initialBalance });

    MemberInfo[] myMemberInfo = custType.GetMember("AddFundsFromPool");

    double thePool = 1000.00;
    Console.WriteLine("The pool is currently ${0}", thePool);
    Console.WriteLine("The original balance of the account instance is ${0}",
                            initialBalance);

    double amountFromPool = 50.00;
    Console.WriteLine("The amount to be subtracted from the pool and added " +
              "to the account is ${0}", amountFromPool);
    
    Console.WriteLine("---");
    Console.WriteLine("Calling {0} ...", myMemberInfo[0].ToString());
    Console.WriteLine("---");

    object[] passMe = new object[] { thePool, amountFromPool };
    Console.WriteLine("The new balance in the account instance is ${0}",
                    custType.InvokeMember("AddFundsFromPool",
                    BindingFlags.InvokeMethod,
                    null, custObj, passMe));
    thePool = (double)passMe[0];
    Console.WriteLine("The new amount in the pool is ${0}", thePool);
   }
}

Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit

 _

Class ParamBuilderDemo
   
   Public Shared Function BuildCustomerDataType() As Type
      
      Dim myDomain As AppDomain = Thread.GetDomain()
      Dim myAsmName As New AssemblyName()
      myAsmName.Name = "MyDynamicAssembly"
      
      Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, _
                        AssemblyBuilderAccess.Run)
      
      Dim myModBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule("MyMod")
      
      Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType("CustomerData", TypeAttributes.Public)
      
      Dim customerNameBldr As FieldBuilder = myTypeBuilder.DefineField("customerName", _
                                GetType(String), _
                                FieldAttributes.Private)
      Dim acctIDBldr As FieldBuilder = myTypeBuilder.DefineField("acctID", _
                                GetType(String), _
                                FieldAttributes.Private)
      Dim balanceAmtBldr As FieldBuilder = myTypeBuilder.DefineField("balanceAmt", _
                                GetType(Double), _
                                FieldAttributes.Private)
      
      Dim myCtorBuilder As ConstructorBuilder = myTypeBuilder.DefineConstructor(MethodAttributes.Public, _
                                CallingConventions.HasThis, _
                                New Type() {GetType(String), _
                                        GetType(String), _
                                        GetType(Double)})
      
      
      Dim ctorIL As ILGenerator = myCtorBuilder.GetILGenerator()
      
      Dim objType As Type = Type.GetType("System.Object")
      Dim objCtor As ConstructorInfo = objType.GetConstructor(New Type(){})
      
      ctorIL.Emit(OpCodes.Ldarg_0)
      ctorIL.Emit(OpCodes.Call, objCtor)
      
      ctorIL.Emit(OpCodes.Ldarg_0)
      ctorIL.Emit(OpCodes.Ldarg_1)
      ctorIL.Emit(OpCodes.Stfld, customerNameBldr)
      
      ctorIL.Emit(OpCodes.Ldarg_0)
      ctorIL.Emit(OpCodes.Ldarg_2)
      ctorIL.Emit(OpCodes.Stfld, acctIDBldr)
      
      ctorIL.Emit(OpCodes.Ldarg_0)
      ctorIL.Emit(OpCodes.Ldarg_3)
      ctorIL.Emit(OpCodes.Stfld, balanceAmtBldr)
      
      ctorIL.Emit(OpCodes.Ret)
      
      ' This method will take an amount from a static pool and add it to the balance.
      ' Note that we are passing the first parameter, fundsPool, by reference. Therefore,
      ' we need to inform the MethodBuilder to expect a ref, by declaring the first
      ' parameter's type to be System.Double& (a reference to a double).

      Dim myMthdBuilder As MethodBuilder = myTypeBuilder.DefineMethod("AddFundsFromPool", _
                                MethodAttributes.Public, _
                                GetType(Double), _
                                New Type() {Type.GetType("System.Double&"), _
                                         GetType(Double)})
      
      Dim poolRefBuilder As ParameterBuilder = myMthdBuilder.DefineParameter(1, _
                            ParameterAttributes.Out, "fundsPool")
      
      Dim amountFromPoolBuilder As ParameterBuilder = myMthdBuilder.DefineParameter(2, _
                            ParameterAttributes.In, "amountFromPool")
      
      Dim mthdIL As ILGenerator = myMthdBuilder.GetILGenerator()
      
      mthdIL.Emit(OpCodes.Ldarg_1)
      mthdIL.Emit(OpCodes.Ldarg_1)
      mthdIL.Emit(OpCodes.Ldind_R8)
      mthdIL.Emit(OpCodes.Ldarg_2)
      mthdIL.Emit(OpCodes.Sub)
      
      mthdIL.Emit(OpCodes.Stind_R8)
      
      mthdIL.Emit(OpCodes.Ldarg_0)
      mthdIL.Emit(OpCodes.Ldarg_0)
      mthdIL.Emit(OpCodes.Ldfld, balanceAmtBldr)
      mthdIL.Emit(OpCodes.Ldarg_2)
      mthdIL.Emit(OpCodes.Add)
      
      mthdIL.Emit(OpCodes.Stfld, balanceAmtBldr)
      
      mthdIL.Emit(OpCodes.Ldarg_0)
      mthdIL.Emit(OpCodes.Ldfld, balanceAmtBldr)
      mthdIL.Emit(OpCodes.Ret)
      
      Return myTypeBuilder.CreateType()

   End Function 'BuildCustomerDataType
    
   
   Public Shared Sub Main()

      Dim custType As Type = Nothing
      Dim custObj As Object = Nothing
      
      Dim custArgTypes() As Type = {GetType(String), GetType(String), GetType(Double)}
      
      ' Call the method to build our dynamic class.
      custType = BuildCustomerDataType()
      
      Console.WriteLine("---")
      
      Dim myCustCtor As ConstructorInfo = custType.GetConstructor(custArgTypes)
      Dim initialBalance As Double = 100.0
      custObj = myCustCtor.Invoke(New Object() {"Joe Consumer", "5678-XYZ", initialBalance})
      
      Dim myMemberInfo As MemberInfo() = custType.GetMember("AddFundsFromPool")
      
      
      Dim thePool As Double = 1000.0
      Console.WriteLine("The pool is currently ${0}", thePool)
      Console.WriteLine("The original balance of the account instance is ${0}", initialBalance)
      
      Dim amountFromPool As Double = 50.0
      Console.WriteLine("The amount to be subtracted from the pool and added " & _
            "to the account is ${0}", amountFromPool)
      
      Console.WriteLine("---")
      Console.WriteLine("Calling {0} ...", myMemberInfo(0).ToString())
      Console.WriteLine("---")
      
      Dim passMe() As Object = {thePool, amountFromPool}
      Console.WriteLine("The new balance in the account instance is ${0}", _
                custType.InvokeMember("AddFundsFromPool", _
                BindingFlags.InvokeMethod, Nothing, custObj, passMe))
      thePool = CDbl(passMe(0))
      Console.WriteLine("The new amount in the pool is ${0}", thePool)

   End Sub

End Class

설명

매개 변수 특성은 메서드 서명과 일치해야 합니다. 매개 변수의 특성을 지정 Out 하는 경우 해당 메서드 매개 변수의 형식이 형식인지 ByRef 확인해야 합니다.

일부 ParameterBuilder 특성은 MSIL(Microsoft 중간 언어)이 런타임에 올바르게 작동하려면 실행 가능한 매개 변수를 사용하여 를 호출 DefineMethod 해야 합니다. 예를 들어 의 매개 변수 1에 대해 ParameterAttributes.Out을 사용하여 을 MethodBuilder정의하는 ParameterBuilder 경우 의 MethodBuilder 매개 변수 1은 Type.GetType("System.String")이 아닌 Type.GetType("System.String&")과 같은 참조여야 합니다.

생성자

ParameterBuilder()

ParameterBuilder 클래스의 새 인스턴스를 초기화합니다.

속성

Attributes

이 매개 변수에 대한 특성을 검색합니다.

IsIn

입력 매개 변수인지 여부를 검색합니다.

IsOptional

이 매개 변수가 선택적인지의 여부를 검색합니다.

IsOut

이 매개 변수가 출력 매개 변수인지 여부를 검색합니다.

Name

이 매개 변수의 이름을 검색합니다.

Position

이 매개 변수에 대한 시그니처 위치를 검색합니다.

메서드

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetToken()

이 매개 변수에 대한 토큰을 검색합니다.

GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
SetConstant(Object)

해당 매개 변수의 기본값을 설정합니다.

SetCustomAttribute(ConstructorInfo, Byte[])

지정된 사용자 지정 특성 blob을 사용하여 사용자 지정 특성을 설정합니다.

SetCustomAttribute(CustomAttributeBuilder)

사용자 지정 특성 작성기를 사용하여 사용자 지정 특성을 설정합니다.

SetCustomAttributeCore(ConstructorInfo, ReadOnlySpan<Byte>)

파생 클래스에서 재정의되는 경우 이 어셈블리에서 사용자 지정 특성을 설정합니다.

SetMarshal(UnmanagedMarshal)
사용되지 않음.

이 매개 변수에 대한 마샬링을 지정합니다.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

명시적 인터페이스 구현

_ParameterBuilder.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

이름 집합을 해당하는 디스패치 식별자 집합에 매핑합니다.

_ParameterBuilder.GetTypeInfo(UInt32, UInt32, IntPtr)

인터페이스의 형식 정보를 가져오는 데 사용할 수 있는 개체의 형식 정보를 검색합니다.

_ParameterBuilder.GetTypeInfoCount(UInt32)

개체에서 제공하는 형식 정보 인터페이스의 수를 검색합니다(0 또는 1).

_ParameterBuilder.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

개체에서 노출하는 메서드와 속성에 대한 액세스를 제공합니다.

적용 대상