是否有可能定义一个扩展运营商的方法? [英] Is it possible define an extension operator method?

查看:118
本文介绍了是否有可能定义一个扩展运营商的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是它可以定义一个扩展方法,该方法在同一时间是操作者?
口要为一个固定的类添加到使用,实际上无法应用公知的操作者的可能性。
,因为我想这样做,这种特殊情况:

is it possible to define an extension method that at the same time is an operator? I want for a fixed class add the possibility to use a known operator that actually can't be applied. For this particular case i want to do this:

   somestring++;  //i really know that this string contains a numeric value

和我不希望传播类型转换的所有代码。
我知道我可以在一个字符串创建包装类,并定义了运营商,但我想知道,如果这种事情是可能避免搜索和替换MySpecialString每个字符串声明。

And i don't want to spread types conversions for all the code. I know that i could create wrapper class over an string and define that operator but i want to know if this kind of thing is possible to avoid search-and-replace every string declaration with MySpecialString.

编辑:作为最有说字符串密封的,所以推导是不可能的,所以我修改衍生来包装,我的失误

Edited: as most have say string is sealed, so derivation isn't possible, so i modify "derived" to "wrapper", my mistake.

推荐答案

没有,这是不可能从类之外的事情。 ++ 经营者应在其内部正在增加类定义。您可以创建自己的类,这将是从字符串转换,将有 ++ 过载,也可以对这个想法忘记和使用常规方法。

No, it is not possible to do from outside of the class. ++ operator should be defined inside class which is being incremented. You can either create your own class which will be convertible from string and will have ++ overload or you can forget about this idea and use regular methods.

这篇关于是否有可能定义一个扩展运营商的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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