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 オブジェクト。

例外

mdBuildernullです。

CreateType() が、外側の型に対して呼び出されました。

次のコード サンプルは、 を使用して作成されたPropertyBuilderプロパティに動的メソッドをsetアタッチする方法をSetSetMethod示しています。

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)

適用対象