2012-02-29 13 views
1

コードを変更できない次のようなクラスがある場合、実行時にEditorAttributeをs1にどのように追加できますか?実行時に(動的に)オブジェクトの特別なプロパティにEditorAttributeを追加する

class TestClass 
{ 
    public String s1 {get;set;} 
    public String s2 {get;set;} 
} 

私はこの方法を試してみましたが、それはあまりにもs2にEditorAttributeエディタを追加し、私はそれを望んでいません。

TypeDescriptor.AddAttributes(
    typeof(String), 
    new EditorAttribute ( 
      typeof(MyUITypeEditor), 
      typeof(UITypeEditor))); 

どうすればいいですか?

答えて

0

CustomTypeDescriptorを使用してクラスの独自のタイプ記述子を実装し、GetPropertiesメソッドをオーバーライドして、独自の属性を追加するカスタムプロパティセットを返すことができます。

このカスタムタイプ記述子を取得したら、そのクラスのインスタンス(TestClassクラスのインスタンスをラップすることができる)をPropertyGridコントロールにバインドできます。

次のような何か:

public class TestClassTypeDescriptor : ICustomTypeDescriptor 
{ 
    private TestClass mInst; 

    public TestClassTypeDescriptor(TestClass inst) 
    { 
    mInst = inst; 
    } 

    //Implement ICustomTypeDescriptor 
} 


PropGridControl.SelectedObject = new TestClassTypeDescriptor(new TestClass()); 

あなたがPropertyDescriptorPropertyDescriptorCollectionの独自の派生バージョンを作成する必要があるかもしれませんが、これらは

を実装するのは非常に簡単です
関連する問題