2013-10-29 37 views
2

我有一個問題,我需要禁用一個特定的規則(在這種情況下CA1819:PropertiesShouldNotReturnArrays)生成的代碼。如果它是我自己的代碼,那麼我會在給定函數中添加一個SuppressMessage屬性,就是這樣。顯然,我不能在生成的代碼中這樣做,因爲它會在下一個版本中丟失。如何在生成的代碼中禁用特定的FxCop規則?

自動生成的代碼:

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

回答

4

爲了解決該問題,可以使用模塊級別禁止。在項目的任何其他來源的文件,下面的語句都可以使用(using指令後必須是正確的):

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

的困難是目標尋找合適的名稱,因爲它必須是完全充分限定字符串。幸運的是,FxCop gui提供了幫助來生成正確的消息:只需右鍵單擊錯誤,選擇「Copy-As」並選擇「Module Level Suppression」

+0

從GUI還可以選擇「Suppress Message」並選擇「在壓縮文件「(而不是」在源「),具有相同的效果。 –

+0

這個選項在哪裏?我在上下文菜單中沒有這個... – PMF

+1

當我在CodeAnalysis窗口中右擊消息時,我有這個選項。但我做*不*具有「複製到」,只是一個「複製」,奇怪。你可能使用舊的FxCop插件而不是現在內置的CodeAnalysis? –

相關問題