From a UITypeEditor, retrieve an attribute applied to the parent of a property in .net
It's not quite 开发者_StackOverflow中文版as simple as the title says but I hope you understand.
Consider this scenario:
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
Next, I put an instance of MyButton into a property grid, expand property MC and edit SubProp2.
From the type editor for SubProp2, I want to get hold of the attribute applied to property MC.
Looking at the context,PropertyDescriptor.ComponentType, we arrive at Type: MyClass. If i look at the attributes applied to MyClass, though, it will only return the attributes applied directly to MyClass, and not the extra attribute applied to the property MC.
I'm struggling to get back to MC so that I can read the attributes applied there. Anyone any ideas?
ETA:
I've answered it below:
If i look at the attributes applied to MyClass, though, it will only return the attributes applied directly to MyClass, and not the extra attribute applied to the property MC.
The attribute added to the MC
property are not "added" to the attributes applied to the MyClass
class... attributes are static metadata, you won't have an extra attribute on MyClass
just because a property of type MyClass
has this attribute. There's no relation between the two.
There's no way for MyClass to retrieve the attributes of the MC property, unless you retrieve them directly from the MyButton
class, through reflection :
Dim prop As PropertyInfo = GetType(MyButton).GetProperty("MC")
Dim attributes As Attribute() = Attribute.GetCustomAttributes(prop)
But you won't be able to retrieve those attributes using TypeDescriptor
, since it can only give you the attributes defined on the type, not on the properties
Actually, further to my comment to Thomas, this is possible without late binding. Here's how:
DirectCast(context, GridItem).Parent.PropertyDescriptor.Attributes.
Voila!
精彩评论