隐式运算符和类型转换器是否等效?

发布于 2024-07-11 08:04:24 字数 251 浏览 10 评论 0原文

在我看来,实现隐式运算符与 TypeConverter 似乎非常容易,因此我假设它们并不等效,因为框架中 TypeConverters 的流行(请参阅扩展 FrameworkElement 的任何内容)。

但为什么? 创建 string->object 和 object->string 隐式运算符并在序列化(XML 和 XAML)中利用这些运算符不是更容易吗?

是亚格尼吗? 单一责任? 因为您无法在接口中指定运算符重载?

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).

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)?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流年已逝 2024-07-18 08:04:24

类型转换器比看起来复杂得多; 类型转换器可以访问有关转换上下文的一系列元数据 - 例如,所涉及的属性和对象。 这用于为每个场景提供自定义选项(想想:链接的下拉菜单,即国家/县/城市/等)。 您还可以在每个属性的基础上指定类型转换器,我在很多地方使用它来提供对各种字符串属性的不同处理。 运算符会同等对待所有字符串。

隐式运算符只知道正在转换的,但具有更好的编译时支持。

或者另一种方式:
TypeConverter是一个框架特性,有框架支持;
运算符(主要)是具有语言支持的语言功能

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

  • 它们提供子属性元数据(想想:在 PropertyGrid 上扩展属性)
  • 他们建议类型的可用选项(想想:PropertyGrid 上的下拉选项)

请注意,它们的使用范围不仅仅是 PropertyGrid,不过 ;-p

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:

  • they provide sub-property metadata (think: expanding properties on PropertyGrid)
  • they suggest available options for a type (think: drop-down choices on PropertyGrid)

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

筑梦 2024-07-18 08:04:24

我不是这方面的专家。

但乍一看,它看起来像 - 您可以在原始类之外提供转换器(相对于隐式运算符)& 也许您可以为同一事物定义多个 TypeConverter 类(如果您想获得相同值的不同视图)。

I am no expert on this.

But at first glance, it looks like - you can provide converters outside of the original class (as against implicit operator) & maybe you can define multiple TypeConverter classes for a same thing (if you want to get different views for the same value).

放血 2024-07-18 08:04:24

隐式运算符很好,但也可能令人困惑。 我认为,当您需要从一种类型转换为另一种类型时,最好是明确的,因为毫无疑问发生了什么。

此外,隐式运算符似乎是为非常相似的事物保留的,并且隐式转换很直观。 但我想无论如何这都是主观的,请使用你最好的判断。

Implicit operators are nice but also they can be confusing. I think that when you need to convert from one type to another it best to be explicit as there is no question as to what is going on.

Also implicit operators seemed to be reserved for things that are very much alike and the implicit conversion is intuitive. But I guess that is all subjective anyhow, use your best judgement.

诗酒趁年少 2024-07-18 08:04:24

我不知道完整的答案,但我有一个怀疑。

我知道 TypeConverters 可以与 Visual Studio 设计器配合使用,这样,如果您为列表或数组等属性提供正确的 TypeConverter,则可以通过设计器设置它们。

隐式运算符也提供这种服务吗? 如果没有,我怀疑这可以回答您的问题:Visual Studio 使用 TypeConverters,因此具有这些项目的控件可以与设计器一起使用。

I don't know the full answer, but I have a suspicion.

I know TypeConverters can work with the Visual Studio designer, such that if you provide the right TypeConverter for properties like Lists or Arrays you can set them via the designer.

Do implicit operators also provide this service? If not, I suspect that answers your question: TypeConverters are used by Visual Studio so controls with those items can work with the designer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文