Suppress CA1062 with fluent validation

2019-06-21 03:57发布

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?

1条回答
干净又极端
2楼-- · 2019-06-21 04:13

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.

查看更多
登录 后发表回答