在库中组织扩展函数
在设计一个流畅且依赖于扩展函数的库时,提供扩展函数的替代行为的方法是什么?
例如,一个进行某种格式化的库:
(123.456879)
.RoundTo(2) // Rounds to 2 places
.ToCurrency() // Applies the appropriate currency symbol
.ToString()
鉴于 RoundTo
和 ToCurrency
将是扩展函数,那么改变 行为的方法是什么? RoundTo
和/或 ToCurrency
?
谢谢, L-
In designing a library that is fluent and that relies on extension functions what would be a way to provide an alternative behavior of an extensions function?
So for instance a library that does some kind of formatting:
(123.456879)
.RoundTo(2) // Rounds to 2 places
.ToCurrency() // Applies the appropriate currency symbol
.ToString()
Given that RoundTo
, and ToCurrency
would be extension functions, what would be a way to change the behavior of RoundTo
and/or ToCurrency
?
Thanks,
L-
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果重写是指在基类中具有虚拟扩展函数并在派生类中重写,则不能 - 扩展函数必须是静态的,并且静态函数不能被重写。
编辑:在您澄清之后,也许您可以编写 库的配置部分(或仅使用应用程序设置)并让您的库读取配置参数。
If by overriding you mean having an extension function virtual in a base class and overridden in a derived class, then you cannot - extension functions must be static, and static functions cannot be overridden.
EDIT: after your clarification, maybe you could write a configuration section for the library (or just use the application settings) and have your library read the configuration parameters.