質問

私は、オブジェクト型がPropertyGridを実装することによりICustomTypeDescriptorに表示される方法をカスタマイズしています。私は、ユーザーがキーと値の単一の辞書に格納されている、独自のカスタムプロパティを作成することができますよ。私は、これらの値のすべてのPropertyDescriptorsを作成し、プロパティグリッドでそれらを表示することができますよ。しかし、私はまたPropertyGridが私のオーバーライドICustomTypeDescriptor.GetProperties方法ではなく、反射によって移入された場合にはそうでない示していたすべてのデフォルトのプロパティを表示したい。

今、私は、オブジェクトの型を取得した後、GetProperties()する方法を知っているが、これはPropertyInfoの配列を返すProperyDescriptorありません。だから、どのように私は、カスタムPropertyInfoと私のコレクションの中に含まれるようにPropertyDescriptorオブジェクトにタイプのPropertyDescriptorsオブジェクトを変換することができますか?

//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);
役に立ちましたか?

解決

PropertyDescriptorCollection props = TypeDescriptor.GetProperties(thisType);

余談:これはあなたのICustomTypeDescriptorのカスタマイゼーションを含むが、それはありません。のでしょうTypeDescriptionProviderを経由して行われたすべてのカスタマイゼーションを含める

(編集) 二余談として - PropertyGridまたはTypeConverterいずれよりもはるかに簡単に - - あなたはまた、ICustomTypeDescriptorを提供することにより、TypeDescriptionProviderを微調整することができます。例:

[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);
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top