简体   繁体   中英

CA1062 & Postsharp required attribute

Consider the following code

 public void AMethodWithAnotherRequiredArgument([Required] string aRequiredArgument)
    {
        Debug.WriteLine("You passed in a string with a length of {0}", aRequiredArgument.Length);
    }

It will trigger a CA1062 (validate arguments of publicmethods), which isn't really valid because PostSharp is performing the validation, in the same way that this

 public void AMethodWithARequiredArgument(string aRequiredArgument)
    {
        Throw.IfNullOrEmpty(aRequiredArgument, "aRequiredArgument");

        Debug.WriteLine("You passed in a string with a length of {0}", aRequiredArgument.Length);
    }

is valid.

Is there any way that I can make FXCop/SCA recognise that I'm covering the method by way of the [Required] attribute, without adding manual exceptions to every method?

dave

There is a short documentation article about using PostSharp with FxCop. It describes that PostSharp actually modifies the build process to make sure that Code Analysis is executed on the assemblies as they were before being enhanced by PostSharp (in obj\\...\\Before-PostSharp folder).

This is required because assemblies processed by PostSharp may cause FxCop to generate too many irrelevant warnings.

The correct solution would be to disable the original FxCop validation rule, and replace it with a custom rule that is aware of PostSharp validation aspects. Currently, this custom rule is not provided with PostSharp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM