Search code examples
visual-studio-2013sitecorefxcop

VS codeanalysis CA1062 fires even after null check


I have a Sitecore project in which I am using visualstudio code analysis. I am using Sitecore method "Assert.ArgumentNotNull" to check for null arguments, However visualstudio code analysis engine doesn't recognize it and show "CA1062 Validate arguments of public methods" message.

Instead of creating a custom rule, Is there a easier way to tell analysis engine that "Assert.ArgumentNotNull" performs null check and message is invalid.

I don't want to suppress the message or disable it.


Solution

  • You can't use Sitecore's Assert class that way and that's why:

    Sitecore Assert class as well as NotNullAttribute and CanBeNullAttribute were made the way ReSharper can understand when it performs its own analysis.

    Definition of Assert.ArgumentNotNull(object, string) method is the following:

    [AssertionMethod]
    public static void ArgumentNotNull([CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] object argument, [CanBeNull] [InvokerParameterName] string argumentName)
    

    All those attributes are defined in Sitecore and R# understands them because of naming conventions.

    Unfortunately, VS code analysis has another naming conventions. ArgumentNotNull should look like this for you:

    public static void ArgumentNotNull([ValidatedNotNull] object argument, string argumentName)
    

    Since you can't modify the Assert class, you can't mark argument parameter with ValidatedNotNullAttribute.