PropertyBuilder.SetSetMethod(MethodBuilder) Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Nastaví metodu, která nastavuje hodnotu vlastnosti.
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
Objekt MethodBuilder
, který představuje metodu, která nastavuje hodnotu vlastnosti.
Výjimky
mdBuilder
je null
.
CreateType() byl volána u ohraničujícího typu.
Příklady
Následující ukázka kódu ukazuje, jak připojit dynamickou metodu set
k vlastnosti vytvořené pomocí PropertyBuilder
.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)
Platí pro
Spolupracujte s námi na GitHubu
Zdroj tohoto obsahu najdete na GitHubu, kde můžete také vytvářet a kontrolovat problémy a žádosti o přijetí změn. Další informace najdete v našem průvodci pro přispěvatele.