PropertyInfoからはプロパティのget
アクセサ/set
アクセサに対応するMethodInfoをそれぞれ個別に取得することができます。 それぞれGetMethodプロパティ/SetMethodプロパティプロパティを参照することでMethodInfoとして取得できます。 読み取り専用/書き込み専用プロパティの場合はnull
/Nothing
が返されます。
PropertyInfoからプロパティのアクセサメソッドを取得する
using System;
using System.Reflection;
class C {
public int P {get; set;}
}
class Sample {
static void Main()
{
var t = typeof(C);
var p = t.GetProperty("P"); // プロパティPのPropertyInfoを取得
var getter = p.GetMethod; // プロパティPのgetアクセサメソッドを取得
var setter = p.SetMethod; // プロパティPのsetアクセサメソッドを取得
Console.WriteLine(getter);
Console.WriteLine(setter);
}
}
実行結果
Int32 get_P() Void set_P(Int32)