How do I disable a specific FxCop rule in generated code?

你离开我真会死。 提交于 2019-12-24 03:34:18

问题


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>

回答1:


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"



来源:https://stackoverflow.com/questions/19662125/how-do-i-disable-a-specific-fxcop-rule-in-generated-code

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