Is it possible to override a type descriptor for an existing .net type?

Or more specifically

1) Is it possible to assign a type descriptor to a property

2) If so, what is the best way to get a type converter at runtime.

Basically, I have configuration objects that are populated with reflection. So far this only works for simple types (string, int, datetime), but I would like to support converting comma separated lists to List.

So far, I have achieved this by deriving the custom type " ConvertableList<T> " from List<T> and decorating it with my custom type converter.

+3
source share
1 answer

You can associate TypeConverter with existing types, for example:

  TypeDescriptor.AddAttributes(typeof(List<int>), new TypeConverterAttribute(typeof(MyTypeConverter))); 

(somewhere during startup)

Then, to get the converter, the standard code should work:

  TypeConverter conv = TypeDescriptor.GetConverter(typeof(List<int>)); 

or

  object obj = new List<int>(); ... TypeConverter conv = TypeDescriptor.GetConverter(obj); 
+7
source

Source: https://habr.com/ru/post/954649/


All Articles