PropertyBuilder.SetGetMethod 方法

设置获取属性 (Property) 值的方法。

**命名空间:**System.Reflection.Emit
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Sub SetGetMethod ( _
    mdBuilder As MethodBuilder _
)
用法
Dim instance As PropertyBuilder
Dim mdBuilder As MethodBuilder

instance.SetGetMethod(mdBuilder)
public void SetGetMethod (
    MethodBuilder mdBuilder
)
public:
void SetGetMethod (
    MethodBuilder^ mdBuilder
)
public void SetGetMethod (
    MethodBuilder mdBuilder
)
public function SetGetMethod (
    mdBuilder : MethodBuilder
)

参数

  • mdBuilder
    MethodBuilder 对象,表示获取属性 (Property) 值的方法。

异常

异常类型 条件

ArgumentNullException

mdBuilder 为 空引用(在 Visual Basic 中为 Nothing)。

InvalidOperationException

已对封闭类型调用了 CreateType

示例

下面的代码示例说明如何使用 SetGetMethod 将一个动态方法附加到用 PropertyBuilder 创建的 get 属性 (Property)。

' 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 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.
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 );

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

PropertyBuilder 类
PropertyBuilder 成员
System.Reflection.Emit 命名空间