从UITypeEditor中,检索应用于.net中属性父级的属性

时间:2010-01-20 22:39:46

标签: c# .net vb.net attributes uitypeeditor

这并不像标题所说的那么简单,但我希望你明白。

考虑这种情况:

Class MyClass

    Property SubProp1
    End Property

    -TypeEditor(whatever)- 
    Property SubProp2
    End Property

End Class

Class MyButton
    Inherits Button

    -MyCustomAttribute-
    Property MC as MyClass
    End Property

End Class

接下来,我将MyButton的一个实例放入属性网格,展开属性MC并编辑SubProp2。

在SubProp2的类型编辑器中,我希望获得应用于属性MC的属性。

查看上下文PropertyDescriptor.ComponentType,我们得到Type:MyClass。 但是,如果我查看应用于MyClass的属性,它将只返回直接应用于MyClass的属性,而不是应用于属性MC的额外属性。

我正努力回到MC,以便我可以阅读那里应用的属性。 任何想法?

ETA:

我已在下面回答:

2 个答案:

答案 0 :(得分:1)

  

但是,如果我查看应用于MyClass的属性,它只会返回直接应用于MyClass的属性,而不会返回应用于属性MC的额外属性。

添加到MC属性的属性未“添加”到应用于MyClass类的属性...属性是静态元数据,您将不会在{{{{{{ 1}}只是因为MyClass类型的属性具有此属性。这两者之间没有关系。

MyClass无法检索MC属性的属性,除非您直接从MyClass类通过反射检索它们:

MyButton

但您无法使用Dim prop As PropertyInfo = GetType(MyButton).GetProperty("MC") Dim attributes As Attribute() = Attribute.GetCustomAttributes(prop) 检索这些属性,因为它只能为您提供在类型上定义的属性,而不是属性

答案 1 :(得分:0)

实际上,在我对托马斯的评论之后,这可能没有后期约束。方法如下:

DirectCast(context, GridItem).Parent.PropertyDescriptor.Attributes.

瞧!