Ben*_*ter 3 .net reflection typeconverter
或者更具体地说
1)我可以为属性分配类型描述符
2)如果是这样,在运行时获取类型转换器的最佳方法是什么.
基本上我有使用反射填充的配置对象.到目前为止,这仅适用于简单类型(string,int,datetime),但我想支持将逗号分隔列表转换为List.
到目前为止,我已经通过ConvertableList<T>从List<T>我的自定义类型转换器派生自定义类型" " 和装饰来实现这一目标.
您可以将TypeConverters与现有类型相关联,例如:
TypeDescriptor.AddAttributes(typeof(List<int>),
new TypeConverterAttribute(typeof(MyTypeConverter)));
Run Code Online (Sandbox Code Playgroud)
(启动时的某个地方)
然后要获得转换器,标准代码应该工作:
TypeConverter conv = TypeDescriptor.GetConverter(typeof(List<int>));
Run Code Online (Sandbox Code Playgroud)
要么:
object obj = new List<int>();
...
TypeConverter conv = TypeDescriptor.GetConverter(obj);
Run Code Online (Sandbox Code Playgroud)