タイプのデフォルトPropertyDescriptorsを取得
-
09-09-2019 - |
質問
私は、オブジェクト型が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);
}
}