Insert custom TypeConverter on a property at runtime, from inside a custom UITypeEditor
I've created a custom UITypeEditor.
Can I possibly insert an attribute that also attaches a TypeConverter to my property from inside the UITypeEditor class?
I've tried the following, but nothing happens, no matter how I twist and turn it:
Attribute[] newAttributes = new Attribute[1];
newAttributes[0] = new TypeConverterAttribute(typeof(BooleanConverter));
Now, the above needs to have the following attached to it somehow:
TypeDescriptor.AddAttributes(context.Instance.PROPERTYNAME, newAttributes);
...but first of all I don't know how to get to the property in question in a generic way, and all code I try just fails. Even if I try to assign the TypeConverter in this manner globally, it fails. (Set开发者_StackOverflow中文版ting it as an attribute on the property itself works though, just to rule out that the bug is in that part.)
TypeDescriptor
allows you to add attributes (at least, as far as System.ComponentModel
is concerned) to types and objects - but not to specific properties. To do that (in a general way would require TypeDescriptionProvider
- however, be advised that this is not a trivial thing to implement, and you need to write all of:
- the provider itself
- an
ICustomTypeDescriptor
- a
PropertyDescriptor
for the things you want to manipulate
It is the PropertyDescriptor
that would allow you to pretend you had additional attributes (or, via override
change the Converter
more directly, without messing with attributes).
精彩评论