1

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

Dave
  • 1,068
  • 12
  • 26
  • 2
    Unfortunately FXCop/SCA sees the code that you wrote, not the code that will eventually end up being compiled and part of your assembly. I would have to say no, but since I'm not 100% positive about this, I'm leaving it as a comment. – Lasse V. Karlsen Jul 04 '14 at 20:22
  • @Lassie thanks. A small clarification - FXCop inspects the CIL version of your code & not the source that you wrote. Unfortunately, this appears to be one level above the IL level that PostSharp works at. – Dave Jul 04 '14 at 20:57
  • So source code -> compiled to assembly -> inspected by FXCop/SCA -> PostSharp ? – Lasse V. Karlsen Jul 04 '14 at 20:59
  • From what I can gather, it's Source Code -> compiled to CIL -> inspected by FXCop/SCA -> Compiled to IL -> PostSharp. That's what I can put together from http://en.wikipedia.org/wiki/FxCop, http://en.wikipedia.org/wiki/Common_Intermediate_Language and http://stackoverflow.com/questions/1165191/postsharp-how-does-it-work Having said that, the guys @ PostSharp appear to be really smart cookies, and I would not be surprised if they have a solution/work around. – Dave Jul 04 '14 at 21:06

1 Answers1

1

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.

AlexD
  • 5,011
  • 2
  • 23
  • 34