Bewerken

Share via


PropertyBuilder.SetSetMethod(MethodBuilder) Method

Definition

Sets the method that sets the property value.

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)

Parameters

mdBuilder
MethodBuilder

A MethodBuilder object that represents the method that sets the property value.

Exceptions

mdBuilder is null.

CreateType() has been called on the enclosing type.

Examples

The following code sample demonstrates how to attach a dynamic method to a set property created with PropertyBuilder using 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)

Applies to