Is it possible to override the type descriptor for an existing .net type?
Or more specifically
1) Can I assign a type descriptor to a property
2) If so, what's the best way of getting the type converter at runtime.
Basically I have configuration objects that are populated using reflection. So far this works just for simple types (string, int, datetime) but I wanted to support converting comma separated lists to List.
So far I've achieved this by deriving a custom type "ConvertableList<T>
" from List<T>
and decorating that with my custom type 开发者_StackOverflow中文版converter.
You can associate TypeConverter
s to existing types like:
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);
精彩评论