2012-02-29 5 views
1

코드를 변경할 수없는 다음과 같은 클래스가있는 경우 런타임시 s1에 EditorAttribute를 어떻게 추가 할 수 있습니까? 런타임에 (동적으로) 객체의 특수 속성에 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의 자신의 파생 버전을 만들어야 할 수도 있지만, 이러한

을 구현하는 것은 매우 간단합니다