我应该始终指定要使用哪个 IFormatProvider 吗?
我尝试在我们产品的几个程序集上运行 FxCop,并且得到了很多“指定 IFormatProvider”规则的匹配项。
碰巧,其中一些是合法的,但它也匹配这样的代码:
Logger.DebugFormat("Appending file {0}", fileName);
可以写成
Logger.DebugFormat(CultureInfo.InvariantCulture, "Appending file {0}", fileName);
第二个变体更难阅读。
那么,实际上是否建议始终指定IFormatProvider
,或者它“只是”规则中使用的启发式的限制?
I tried running FxCop on a few assemblies of our product, and I get lots and lots of matches of the "Specify IFormatProvider" rule.
As it happens, some of those are legitimate, but it also matches code like this:
Logger.DebugFormat("Appending file {0}", fileName);
Which can be written as
Logger.DebugFormat(CultureInfo.InvariantCulture, "Appending file {0}", fileName);
The second variant is much harder to read.
So, is it actually recommended to always specifiy the IFormatProvider
or is it "just" a limitation of the heuristic used in the rule?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它仅适用于具有
IFormatProvider
重载的方法。为了解决这个问题,我有两个静态类,
InvariantText
和CulturedText
,分别处理不变区域性和当前区域性中的字符串。例如,我在每个类中都有一个 Format 方法。这样,我就可以进行文化中立和文化感知的格式化,而不必每次都指定IFormatProvider
。示例:
InvariantText.Format
和CulturedText.Format
只是String.Format
方法的包装器,因此同样返回字符串。您甚至可以使用此模式来包装需要区域性中立和区域性特定字符串的其他函数。例如,创建两个方法:
InvariantLog
和CulturedLog
,它们在问题中包装对Logger.DebugFormat
的调用,并采用适当的IFormatProvider< /code> 在每种情况下。
It only applies to methods with an
IFormatProvider
overload.To deal with this problem, I have two static classes,
InvariantText
andCulturedText
, that work with strings in the invariant culture and current culture, respectively. For example, I have a Format method in each class. This way, I can do culture-neutral and culture-aware formatting without having to specify anIFormatProvider
each time.Example:
InvariantText.Format
andCulturedText.Format
are simply wrappers to theString.Format
method, and accordingly likewise return strings.You can even use this pattern to wrap other functions that require culture-neutral and culture-specific strings. For example, create two methods,
InvariantLog
andCulturedLog
that wrap calls toLogger.DebugFormat
in your question and take the appropriateIFormatProvider
in each case.这取决于。您知道应用程序的使用方式和位置,因此请考虑遵循 MSDN建议:
PS:我相信
FxCop
遵循第三条规则,让您自己指定正确的文化。It depends. You are aware how and where application will be used, so please consider following MSDN recommendations:
PS: I believe
FxCop
follows the third rule and let you specify the right culture yourself.该规则并不是代码的唯一读者。如果没有显式指定格式设置区域性,维护开发人员将无法区分故意回退到默认格式设置区域性(大多数情况下为 CurrentCulture)或可能导致格式不正确的遗漏。如果您不喜欢冗长的内容,请考虑使用包装方法,例如 Peter O.
The rule is not the only reader of your code. If you do not explicitly specify a formatting culture, a maintenance developer will not be able to distinguish between a deliberate fallback to the default formatting culture (CurrentCulture in most cases) or an omission that may lead to incorrect formatting. If you don't like the verbosity, consider using wrapper methods like those proposed by Peter O.