Insert custom TypeConverter on a property at runtime, from inside a custom UITypeEditor

≯℡__Kan透↙ 提交于 2019-12-10 22:18:25

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/2535647/insert-custom-typeconverter-on-a-property-at-runtime-from-inside-a-custom-uityp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!