CA1026(所有参数应有默认值)和扩展方法
前提
当使用具有 C# 可选参数的代码分析(或 fxCop)时,您可能会收到 的警告CA1026。这样做的简单原因1是没有为所有参数提供默认值。
下面的声明正确地生成了此警告。
public Color GetColor(bool red, bool blue = true, bool green = true)
但是,在一种情况下,您无法为所有参数提供默认值,即扩展方法。因此,下面的声明由于第一个参数而生成警告:
public static bool ValidateRules(this string s, Rules rules = Rules.Default)
编译器不会让您在该参数上指定默认值,因此唯一的两个解决方案是:
- 忽略警告,我不喜欢这样做,因为它会导致错误做法。
- 不使用扩展方法,我不喜欢这样做,因为我发现扩展方法使代码更具可读性。
问题
- 以上两个选项是唯一的吗 有办法解决这个问题吗?
- 是 fxCop/代码 检查中的分析不正确?
Premise
When using code analysis (or fxCop) with C# optional parameters you can get a warning of CA1026. The short reason1 for this is not suppling all parameters with a default value.
The declaration below rightly generates this warning
public Color GetColor(bool red, bool blue = true, bool green = true)
However there is a situation where you could not supply all parameters with a default, and that is extension methods. So the declaration below generates the warning because of the first parameter:
public static bool ValidateRules(this string s, Rules rules = Rules.Default)
The compiler will not let you specify a default value on the this parameter so the only two solutions is to:
- Ignore the warning, which I do not like doing because it leads to bad practices.
- Not use extension methods, which I do not like doing because I find extension methods make the code more readible.
Questions
- Are the above two options the only
way to solve this? - Is fxCop/Code
Analysis incorrect in it's check?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它并不是警告您没有所有参数的默认值,而是警告您根本使用可选参数。
我个人会禁用这个特定的警告。当小心使用时,我认为可选参数是可以的。您应该仔细考虑它们,尤其是默认参数值的版本控制和,以及不支持它们的语言(包括 v4 之前的 C#),但在许多环境中,缺点实际上并不存在一个问题 - 与在各处指定重载相比,您最终可以得到更简单的代码。
It's not warning you for not having defaults for all parameters - it's warning you for using optional parameters at all.
Personally I would disable this particular warning. When used with care, I think optional parameters are fine. You should think carefully about them particularly in terms of versioning of the default parameter value and in terms of languages which don't support them (including C# before v4) but in many environments the downsides really aren't an issue - and you can end up with much simpler code than by specifying overloads all over the place.
我在 Jon Skeet 的答案中遗漏的一个论点也是关于可维护性:默认值总是用 IL(中间语言)中的值填充。如果您使用外部库,这就是一个问题。
以下是重现一个简单示例的步骤:
ClassLibrary
项目Program.cs
和 Class1.cs 中
如果运行它,您将看到“http” ,正如预期的那样。
您仍然会看到
http
!使用 ILSpy,您可以看到http
已硬编码在控制台应用程序中。在这种情况下,如果开发人员认为将默认值中的“http”替换为“https”是安全的,则可能会导致安全问题。
因此,如果更新了外部库,请务必再次编译您的代码。或者只是不使用默认值。
只需创建一个单独的方法:
An argument that I am missing in Jon Skeet's answer is also about maintainability: Default values are always filled in with it's value in the IL (intermediate language). This is an issue if you're using external libraries.
Here are steps to reproduce a simple example:
ClassLibrary
project to itProgram.cs
and in your Class1.cs
If you run it you will see 'http', as expected.
You will still see
http
! With ILSpy you can see thathttp
is hardcoded in the console app.In this case this could lead to a security issue if the developer thinks he is safe by replacing the "http" to "https" in the default value.
So if external libraries are updated always compile your code again. Or just don't use default values.
Just create a separate method:
您可以根据具体情况抑制警告。
You can suppress the warning on a case-by-case basis.