PropertyBuilder.SetSetMethod(MethodBuilder) Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Ustawia metodę, która ustawia wartość właściwości.
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)
Parametry
- mdBuilder
- MethodBuilder
MethodBuilder
Obiekt reprezentujący metodę, która ustawia wartość właściwości.
Wyjątki
mdBuilder
to null
.
CreateType() został wywołany dla otaczającego typu.
Przykłady
W poniższym przykładzie kodu pokazano, jak dołączyć metodę dynamiczną do właściwości utworzonej set
PropertyBuilder
za pomocą polecenia 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)