다음을 통해 공유


PropertyBuilder.SetSetMethod(MethodBuilder) 메서드

정의

속성 값을 설정하는 메서드를 설정합니다.

public:
 void SetSetMethod(System::Reflection::Emit::MethodBuilder ^ mdBuilder);
public void SetSetMethod (System.Reflection.Emit.MethodBuilder mdBuilder);
member this.SetSetMethod : System.Reflection.Emit.MethodBuilder -> unit
Public Sub SetSetMethod (mdBuilder As MethodBuilder)

매개 변수

mdBuilder
MethodBuilder

속성 값을 설정하는 메서드를 나타내는 MethodBuilder 개체입니다.

예외

mdBuilder이(가) null인 경우

바깥쪽 형식에서 CreateType()을 호출한 경우

예제

다음 코드 샘플에서는 를 사용하여 SetSetMethod만든 PropertyBuilder 속성에 동적 메서드를 set 연결하는 방법을 보여 줍니다.

TypeBuilder^ helloWorldTypeBuilder = myModule->DefineType( "HelloWorld", TypeAttributes::Public );

// Define a private String field named "m_greeting" in "HelloWorld" class.
FieldBuilder^ greetingFieldBuilder = helloWorldTypeBuilder->DefineField( "m_greeting", String::typeid, FieldAttributes::Private );

// Create constructor args and define constructor.
array<Type^>^constructorArgs = {String::typeid};
ConstructorBuilder^ constructor = helloWorldTypeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, constructorArgs );

// Generate IL code for the method. The constructor stores its argument in the private field.
ILGenerator^ constructorIL = constructor->GetILGenerator();
constructorIL->Emit( OpCodes::Ldarg_0 );
constructorIL->Emit( OpCodes::Ldarg_1 );
constructorIL->Emit( OpCodes::Stfld, greetingFieldBuilder );
constructorIL->Emit( OpCodes::Ret );

// Define property Greeting.
PropertyBuilder^ greetingPropertyBuilder = helloWorldTypeBuilder->DefineProperty( "Greeting", PropertyAttributes::None, String::typeid, nullptr );

// Define the 'get_Greeting' method.
MethodBuilder^ getGreetingMethod = helloWorldTypeBuilder->DefineMethod( "get_Greeting", static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::HideBySig | MethodAttributes::SpecialName), String::typeid, nullptr );

// Generate IL code for 'get_Greeting' method.
ILGenerator^ methodIL = getGreetingMethod->GetILGenerator();
methodIL->Emit( OpCodes::Ldarg_0 );
methodIL->Emit( OpCodes::Ldfld, greetingFieldBuilder );
methodIL->Emit( OpCodes::Ret );
greetingPropertyBuilder->SetGetMethod( getGreetingMethod );


// Define the set_Greeting method.
array<Type^>^methodArgs = {String::typeid};
MethodBuilder^ setGreetingMethod = helloWorldTypeBuilder->DefineMethod( "set_Greeting", static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::HideBySig | MethodAttributes::SpecialName), void::typeid, methodArgs );

// Generate IL code for set_Greeting method.
methodIL = setGreetingMethod->GetILGenerator();
methodIL->Emit( OpCodes::Ldarg_0 );
methodIL->Emit( OpCodes::Ldarg_1 );
methodIL->Emit( OpCodes::Stfld, greetingFieldBuilder );
methodIL->Emit( OpCodes::Ret );
greetingPropertyBuilder->SetSetMethod( setGreetingMethod );
// Define a public class named "HelloWorld" in the assembly.
TypeBuilder helloWorldTypeBuilder = myModule.DefineType("HelloWorld", TypeAttributes.Public);
// Define a private String field named "m_greeting" in "HelloWorld" class.
FieldBuilder greetingFieldBuilder = helloWorldTypeBuilder.DefineField("m_greeting",
                                          typeof(String), FieldAttributes.Private);
// Create constructor args and define constructor.
Type[] constructorArgs = { typeof(String) };
ConstructorBuilder constructor = helloWorldTypeBuilder.DefineConstructor(
   MethodAttributes.Public, CallingConventions.Standard, constructorArgs);

// Generate IL code for the method. The constructor stores its argument in the private field.
ILGenerator constructorIL = constructor.GetILGenerator();
constructorIL.Emit(OpCodes.Ldarg_0);
constructorIL.Emit(OpCodes.Ldarg_1);
constructorIL.Emit(OpCodes.Stfld, greetingFieldBuilder);
constructorIL.Emit(OpCodes.Ret);
// Define property Greeting.
PropertyBuilder greetingPropertyBuilder = helloWorldTypeBuilder.DefineProperty(
                         "Greeting",PropertyAttributes.None,typeof(string),null);

// Define the 'get_Greeting' method.
MethodBuilder getGreetingMethod = helloWorldTypeBuilder.DefineMethod("get_Greeting",
   MethodAttributes.Public|MethodAttributes.HideBySig|MethodAttributes.SpecialName,
   typeof(String),null);
// Generate IL code for 'get_Greeting' method.
ILGenerator methodIL = getGreetingMethod.GetILGenerator();
methodIL.Emit(OpCodes.Ldarg_0);
methodIL.Emit(OpCodes.Ldfld, greetingFieldBuilder);
methodIL.Emit(OpCodes.Ret);
greetingPropertyBuilder.SetGetMethod(getGreetingMethod);

// Define the set_Greeting method.
Type[] methodArgs = {typeof(string)};
MethodBuilder setGreetingMethod = helloWorldTypeBuilder.DefineMethod("set_Greeting",
   MethodAttributes.Public|MethodAttributes.HideBySig|MethodAttributes.SpecialName,
   typeof(void), methodArgs);
// Generate IL code for set_Greeting method.
methodIL = setGreetingMethod.GetILGenerator();
methodIL.Emit(OpCodes.Ldarg_0);
methodIL.Emit(OpCodes.Ldarg_1);
methodIL.Emit(OpCodes.Stfld,greetingFieldBuilder);
methodIL.Emit(OpCodes.Ret);
greetingPropertyBuilder.SetSetMethod(setGreetingMethod);
' Define a public class named "HelloWorld" in the assembly.
Dim helloWorldTypeBuilder As TypeBuilder = myModule.DefineType("HelloWorld", _
                                                                     TypeAttributes.Public)
' Define a private String field named "m_greeting" in "HelloWorld" class.
Dim greetingFieldBuilder As FieldBuilder = helloWorldTypeBuilder.DefineField _
                                 ("m_greeting", GetType(String), FieldAttributes.Private)
' Create constructor args and define constructor.
Dim constructorArgs As Type() = {GetType(String)}
Dim constructor As ConstructorBuilder = helloWorldTypeBuilder.DefineConstructor _
                     (MethodAttributes.Public, CallingConventions.Standard, constructorArgs)

' Generate IL code for the method. The constructor stores its argument in the private field.
Dim constructorIL As ILGenerator = constructor.GetILGenerator()
constructorIL.Emit(OpCodes.Ldarg_0)
constructorIL.Emit(OpCodes.Ldarg_1)
constructorIL.Emit(OpCodes.Stfld, greetingFieldBuilder)
constructorIL.Emit(OpCodes.Ret)
' Define property Greeting.
Dim greetingPropertyBuilder As PropertyBuilder = helloWorldTypeBuilder.DefineProperty _
                           ("Greeting", PropertyAttributes.None, GetType(String), Nothing)

' Define the 'get_Greeting' method.
Dim getGreetingMethod As MethodBuilder = helloWorldTypeBuilder.DefineMethod("get_Greeting", _
                        MethodAttributes.Public Or MethodAttributes.HideBySig Or _
                        MethodAttributes.SpecialName, GetType(String), Nothing)
' Generate IL code for 'get_Greeting' method.
Dim methodIL As ILGenerator = getGreetingMethod.GetILGenerator()
methodIL.Emit(OpCodes.Ldarg_0)
methodIL.Emit(OpCodes.Ldfld, greetingFieldBuilder)
methodIL.Emit(OpCodes.Ret)
greetingPropertyBuilder.SetGetMethod(getGreetingMethod)

' Define the set_Greeting method.
Dim methodArgs As Type() = {GetType(String)}
Dim setGreetingMethod As MethodBuilder = helloWorldTypeBuilder.DefineMethod _
      ("set_Greeting", MethodAttributes.Public Or MethodAttributes.HideBySig Or _
                              MethodAttributes.SpecialName, Nothing, methodArgs)

' Generate IL code for set_Greeting method.
methodIL = setGreetingMethod.GetILGenerator()
methodIL.Emit(OpCodes.Ldarg_0)
methodIL.Emit(OpCodes.Ldarg_1)
methodIL.Emit(OpCodes.Stfld, greetingFieldBuilder)
methodIL.Emit(OpCodes.Ret)
greetingPropertyBuilder.SetSetMethod(setGreetingMethod)

적용 대상