开发者

Getting the converter for the type

开发者 https://www.devze.com 2023-01-16 03:47 出处:网络
In the MSDN I\'ve read this about EnumConverter: You should never create an instance of an EnumConverter. Instead, call the GetConverter method of the TypeDescriptor class. For more information, see

In the MSDN I've read this about EnumConverter:

You should never create an instance of an EnumConverter. Instead, call the GetConverter method of the TypeDescriptor class. For more information, see the examples in开发者_如何学编程 the TypeConverter base class.

Does anybody know why and is it true for my own implemented converters?

For example, I have class GradientColor and converter GradientColorConverter. Should I write

new GradientColorConverter().ConvertFrom(colorString) 

or

TypeDescriptor.GetConverter(typeof(GradientColor)).ConvertFrom(colorString);

Actually It works in both ways, but which is better?


I think the latter TypeDescriptor.GetConverter(typeof(GradientColor)) because it allows other converters to add or extend the type converter system when the code is run in a different context (like custom control run in another application with its own custom typeconverters).


The latter. If you change the type converter class you code will still work. Decoupling is good.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号