views:

108

answers:

1

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. (Setting it as an attribute on the property itself works though, just to rule out that the bug is in that part.)

+2  A: 

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).

Marc Gravell
Ah yeah, that explains it. And since I wanted to avoid recreating the PropertyDescriptor, I'll probably stick to having to use attributes the old way. Sounds like too much hassle to save a single line of code on a few properties. Thanks!
Pedery