9

PropertyGridを実装して、オブジェクト タイプの表示方法をカスタマイズしていますICustomTypeDescriptor。キーと値の単一の辞書に格納される独自のカスタム プロパティをユーザーが作成できるようにしています。これらの値のすべてを作成PropertyDescriptorsし、プロパティ グリッドで表示できます。ただし、オーバーライドメソッドPropertyGridではなく、リフレクションによって設定された場合に表示されるすべての既定のプロパティも表示したいと考えています。ICustomTypeDescriptor.GetProperties

これで、オブジェクトの型を取得してから を取得する方法がわかりましたが、これはnotGetProperties()の配列を返します。では、そのタイプのオブジェクトをオブジェクトに変換して、カスタムでコレクションに含めるにはどうすればよいでしょうか?PropertyInfoProperyDescriptorPropertyInfoPropertyDescriptorPropertyDescriptors

//gets the local intrinsic properties of the object
Type thisType = this.GetType();
PropertyInfo[] thisProps = thisType.GetProperties();

//this line obviously doesn't work because the propertydescriptor 
//collection needs an array of PropertyDescriptors not PropertyInfo
PropertyDescriptorCollection propCOl = 
    new PropertyDescriptorCollection(thisProps);
4

1 に答える 1

15
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(thisType);

余談ですICustomTypeDescriptorが、これにはカスタマイズ含まれませんが、 を介して行われたカスタマイズは含まれますTypeDescriptionProvider

(編集) 余談ですが、次のいずれかよりもはるかに単純なPropertyGrida を指定して調整することもできます。たとえば:TypeConverterICustomTypeDescriptorTypeDescriptionProvider

[TypeConverter(typeof(FooConverter))]
class Foo { }

class FooConverter : ExpandableObjectConverter
{
    public override PropertyDescriptorCollection GetProperties(
       ITypeDescriptorContext context, object value, Attribute[] attributes)
    {
        // your code here, perhaps using base.GetPoperties(
        //    context, value, attributes);
    }
}
于 2009-04-09T20:29:32.260 に答える