リフレクション コードを少し変更してみてください。1 つには、オブジェクトと、property具体的に必要な の両方を参照する必要があります。Propertyは と同じではないことに注意してくださいField。
MyObject.GetType().GetProperty("Enabled").SetValue(MyObject, bEnabled, null);
MyObjectボタンやフォームなど、どんなタイプでも使用します...次に、プロパティを name で参照しEnabled、それを に対して元に戻しますMyObject。
事前にプロパティを取得したい場合は、インスタンスを変数に格納できますが、プロパティはフィールドではないことに注意してください。
PropertyInfo[] piSet = MyObject.GetType().GetProperties();
を使用thisしてプロパティ セットを取得できますが、有効/無効にしようとしているコントロールthisと同じでない場合はお勧めできません。Type
編集を追加
質問を読み直した後、私はこれを理解しました。あなたが望んでいるように見えるのは、多層リフレクションとジェネリックです。探しているコントロールは、「this」に関連付けられた Field です。あなたができることは、これらの線に沿った何かです。
Type theType = this.GetType();
FieldInfo[] fi = theType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
foreach ( FieldInfo f in fi)
{
//Do your own object identity check
//if (f is what im looking for)
{
Control c = f.GetValue(this) as Control;
c.Enabled = bEnabled;
}
//Note: both sets of code do the same thing
//OR you could use pure reflection
{
f.GetValue(this).GetType().GetProperty("Enabled").SetValue(f.GetValue(this), bEnabled, null);
}
}