通过流畅的验证抑制CA1062 [英] Suppress CA1062 with fluent validation

查看:105
本文介绍了通过流畅的验证抑制CA1062的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个流畅,可扩展的验证助手,例如:

I have a fluent, extensible validation helper like:

Assert.That(aParameter).IsNotNull();

它是可扩展的,因为That方法实际上是通用的(That< T>),并且使用隐式类型返回通用IAssertCondition< T>目的。 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.

无论如何,使用这种方法来验证传递给方法的参数的问题是,我收到CA1062警告,指示我之前验证参数

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.

我阅读了Eric Smith的帖子(此处)关于使用ValidatedNotNullAttribute通知FxCop该参数已通过验证,但我看不到如何使用我使用的流畅接口来完成此操作

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?

推荐答案

在这种情况下,您唯一可以添加属性的地方是 That< T> 的参数方法。不幸的是,虽然这会阻止CA1062触发,但可能会导致误报,因为您需要调用的不仅仅是 That< T> 才能真正实现非空验证。如果您想使用代码分析以识别验证助手的方式正确检查参数验证,则几乎必须编写自己的规则来替换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 just That<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.

这篇关于通过流畅的验证抑制CA1062的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆