Suppress CA1062 with fluent validation

ε祈祈猫儿з 提交于 2019-12-14 00:58:05

问题


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:


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.



来源:https://stackoverflow.com/questions/8244958/suppress-ca1062-with-fluent-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!