是隐含的经营者和类型转换器相同呢? [英] Are implicit operators and TypeConverters equivalent?

查看:132
本文介绍了是隐含的经营者和类型转换器相同呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,它很容易实现一个隐含的运营商与一个类型转换器,所以我假设他们是因为类型转换器在框架prevalence不相等(看到任何扩展FrameworkElement的)。

It seems to me its very easy to implement an implicit operator versus a TypeConverter, so I'm assuming they aren't equivalent because of the prevalence of TypeConverters in the framework (see anything that extends FrameworkElement).

但是,为什么?那岂不是一直更容易创建与字符串>对象和对象 - >字符串隐含运营商,并采取那些序列化(XML和XAML)的优势在哪里?

But why? Wouldn't it have been much easier to create string->object and object->string implicit operators and take advantage of those in serialization (both XML and XAML)?

难道YAGNI?单责任?因为你不能指定接口的操作符重载?

Is it YAGNI? Single responsibility? Because you can't specify operator overloads in interfaces?

推荐答案

类型转换器有很多复杂得多,他们似乎;一个类型转换器有转换获得各种元数据的有关的上下文的 - 例如,属性和对象所涉及的。这是用来提供每个场景自定义选项(认为:联下拉菜单,即国家/县/市/等)。您还可以指定在每个属性的基础类型转换器,我使用的很多地方可以提供不同的处理各种字符串属性。操作员会来对待所有的字符串。

Type converters are a lot more complex than they seem; a type-converter has access to a range of meta-data about the context of the conversion - for example, the property and object that are involved. This is used to provide custom options per scenario (think: linked drop-downs, i.e. Country / County / City / etc). You can also specify the type-converter on a per-property basis, which I use in lots of places to provide different handling of various string properties. An operator would treat all strings identically.

这是隐含的操作者只知道关于的正在转换,但更大的编译时支持。

An implicit operator only knows about the value that is being converted, but has far greater compile-time support.

或者另一种方式: 类型转换器是框架支撑框架的功能; 运营商(主要)与语言支持语言功能

Or another way: TypeConverter is a framework feature with framework support; operators are (primarily) a language feature with language support

要添加更多 - 型转换器(尽管名称)不只是转换:

To add more - type-converters (despite the name) don't just convert:

  • 在他们提供的子属性的元数据(认为:在扩大属性的PropertyGrid
  • 他们提出了一种可用的选项(认为:下拉选择上的PropertyGrid
  • they provide sub-property metadata (think: expanding properties on PropertyGrid)
  • they suggest available options for a type (think: drop-down choices on PropertyGrid)

请注意它们被用在更多的地方不仅仅是的PropertyGrid ,虽然;-p

Note they are used in more places than just PropertyGrid, though ;-p

这篇关于是隐含的经营者和类型转换器相同呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆