PropertyBuilder.SetGetMethod(MethodBuilder) 方法

定义

设置获取属性值的方法。

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

参数

mdBuilder
MethodBuilder

MethodBuilder 对象,表示获取属性值的方法。

例外

mdBuildernull

已对封闭类型调用了 CreateType()

示例

下面的代码示例演示如何将动态方法get附加到使用 SetGetMethod创建PropertyBuilder的属性。

// 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 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 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)

适用于