开发者

Organizing Extensions Functions in a Lib

开发者 https://www.devze.com 2023-02-10 16:34 出处:网络
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?

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.45687开发者_如何学Go9)
   .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-


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.

0

精彩评论

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