1

我有一个带有 SelectedObject 类型对象的 PropertyGrid class1

我正在实现对象的ICustomTypeDescriptor接口class1,并且我PropertyDescriptorCollection从另一个对象中获取一个class2,并且需要class2 PropertyDescriptors在 PropertyGrid 和class1 PropertyDescriptors.

我在 PropertyGrid 中显示以下错误class2 PropertyDescriptors

对象与目标类型不匹配。

这是我的代码,无需class2 PropertyDescriptors

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
    PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(this);
    var propertyDescriptors = new List<PropertyDescriptor>();
    for (int i = 0; i < propertyDescriptorCollection.Count; i++)
    {
        propertyDescriptors.Add(propertyDescriptorCollection[i]);
    }
    return new PropertyDescriptorCollection(propertyDescriptors.ToArray());
}

这是我正在处理的代码:

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
    PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(this);
    var propertyDescriptors = new List<PropertyDescriptor>();
    for (int i = 0; i < propertyDescriptorCollection.Count; i++)
    {
        propertyDescriptors.Add(propertyDescriptorCollection[i]);
    }
    var class2PropertyDescriptorCollection = TypeDescriptor.GetProperties(class2);
    for (int i = 0; i < class2PropertyDescriptorCollection.Count; i++)
    {
        propertyDescriptors.Add(class2PropertyDescriptorCollection[i]);
    }
    return new PropertyDescriptorCollection(propertyDescriptors.ToArray());
}

当from显示在 PropertyGrid 中时,如何显示PropertyDescriptorsfrom ?class2PropertyDescriptorsclass1

4

1 回答 1

0

您已将 propertyDescriptorCollection 声明为类型 PropertyDescriptorCollection,但已将 var 用于 class2PropertyDescriptorCollection。如果您尝试以下操作会怎样:

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
    PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(this);
    var propertyDescriptors = new List<PropertyDescriptor>();
    for (int i = 0; i < propertyDescriptorCollection.Count; i++)
    {
        propertyDescriptors.Add(propertyDescriptorCollection[i]);
    }
    PropertyDescriptorCollection class2PropertyDescriptorCollection = TypeDescriptor.GetProperties(class2);
    for (int i = 0; i < class2PropertyDescriptorCollection.Count; i++)
    {
        propertyDescriptors.Add(class2PropertyDescriptorCollection[i]);
    }
    return new PropertyDescriptorCollection(propertyDescriptors.ToArray());
}
于 2016-04-06T00:12:34.617 回答