通过流畅的验证抑制 CA1062
我有一个流畅的、可扩展的验证助手,例如:
Assert.That(aParameter).IsNotNull();
它是可扩展的,因为 That 方法实际上是通用的 (That
不管怎样,使用这种方法来验证传递给方法的参数的问题是,我收到 CA1062 警告,指示我在使用参数之前验证参数,当然,我已经在这样做了。
我阅读了 Eric Smith 的文章(此处),内容涉及使用 ValidatedNotNullAttribute 来通知 FxCop该参数正在验证,但我不知道如何使用我描述的流畅界面来完成此操作。
我可以选择哪些选项,以便代码分析能够识别上述语句满足要求并且不会出现警告?
I have a fluent, extensible validation helper like:
Assert.That(aParameter).IsNotNull();
It is extensible because the That method is actually generic (That<T>) and uses implicit typing to return a generic IAssertCondition<T> object. IsNotNull is actually an extension method.
Anyway, the problem using this approach to validate the parameters passed into a method is that I get CA1062 warnings instructing me to validate the arguments before using them which, of course, I am already doing.
I read Eric Smith's post (here) about using a ValidatedNotNullAttribute to inform FxCop that the argument is being validated but I don't see how I can accomplish this using the fluent interface I've described.
What are my options so that Code Analysis will recognize that the above statement satisfies the requirements and the warning will not appear?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,您可以添加属性的唯一位置是
That
方法的参数。不幸的是,虽然这会阻止 CA1062 触发,但它可能会导致漏报,因为您需要调用的不仅仅是That
来实际实现“非空”验证。如果您想使用代码分析以识别验证助手的方式正确检查参数验证,您几乎必须编写自己的规则来替换 CA1062。The only place you could add the attribute in this case is on the parameter of the
That<T>
method. Unfortunately, while that would prevent CA1062 from firing, it could lead to false negatives since you need to call more than justThat<T>
to actually implement a "not null" verification. If you want to use Code Analysis to properly check for parameter validation in a manner that recognizes your validation helper, you're pretty much going to have to write your own rule to replace CA1062.