Domanda

I had a problem where I needed to disable a certain rule (in this case CA1819:PropertiesShouldNotReturnArrays) for generated code. If it was my own code, I would have just added a SuppressMessage Attribute to the given function and that's it. Obviously, I can't do that in generated code because it will be lost on the next build.

Automatically generated code:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class ListViewTable {

    private ListViewTableRow[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Row", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public ListViewTableRow[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

The Items property generates

 <Message TypeName="PropertiesShouldNotReturnArrays" Category="Microsoft.Performance" CheckId="CA1819" Status="Active" Created="2013-10-29 14:47:04Z" FixCategory="Breaking">
         <Issue Certainty="50" Level="Warning" Path="D:\Projects\FlightPlanning\src\Core\FpesCustomControls" File="AoiSchema.cs" Line="32">Change 'ListViewTable.Items' to return a collection or make it a method.</Issue>
        </Message>
È stato utile?

Soluzione

To solve the problem, module-level suppressions can be used. In any other source file of the project, the following statement can be used (must be right after the using directives):

[module: SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "Generated code",
Scope = "member", Target = "FlightPlanning.AoiSchema.ListViewTable.#Items")]

The difficulty is finding the right name for the Target, because it must be an exact fully qualified string. Luckily, the FxCop gui offers help to generate the correct message: Just right-click the error, select "Copy-As" and choose "Module level Suppression"

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top