如何在生成的代码中禁用特定的FxCop规则?

时间:2013-10-29 15:13:36

标签: .net fxcop

我遇到了一个问题,我需要为生成的代码禁用某个规则(在本例中为CA1819:PropertiesShouldNotReturnArrays)。如果它是我自己的代码,我会在给定函数中添加一个SuppressMessage属性,就是这样。显然,我不能在生成的代码中这样做,因为它会在下一个版本中丢失。

自动生成的代码:

[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;
        }
    }
}

Items属性生成

 <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 个答案:

答案 0 :(得分:4)

要解决此问题,可以使用模块级抑制。在项目的任何其他源文件中,可以使用以下语句(必须在using指令之后):

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

难点在于找到Target的正确名称,因为它必须是完全合格的字符串。幸运的是,FxCop gui提供了生成正确消息的帮助:只需右键单击错误,选择“Copy-As”并选择“Module level Suppression”

相关问题