문제

객체 유형이 어떻게 표시되는지 사용자 정의하고 있습니다. 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 a 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