2013-07-29 69 views
6

我試圖將我們的自定義FxCop(=代碼分析)規則從Visual Studio 2010遷移到Visual Studio 2012.FxCop自定義規則沒有在VS2012中的名稱(但「官方」規則有一個名稱)

到目前爲止,他們的工作除了一件事罰款:雖然顯示了微軟的規則的名稱(>Code AnalysisView - - >Other windows),只有CheckIdResolution顯示,他們的名字是在結果窗口空白

The name isn't visible on the right of the CheckId on the custom rule, but is visible on the Microsoft rule.

什麼是斯特蘭GE,是名字是在規則集編輯器可見:

The name is visible on both rules in the ruleset editor

什麼是錯我的規則?

詳細

下面是這兩個規則如何在MSBuild的輸出顯示(有可疑之處,但我不明白爲什麼我有不同的消息):

6>c:\Users\Me\MySolution\MyProject\Program.cs(15): warning : CA1804 : Microsoft.Performance : 'Foo<T>.SomeMethod()' declares a variable, 'z', of type 'int', which is never used or is only assigned to. Use this variable or remove it. 
6>MSBUILD : warning : CF1001 : CustomRules.ThreadSafety : The public Public Field "Tests.Program.Foo" must be preceded of readonly 

這裏是規則聲明在我的規則的XML文件中:

<?xml version="1.0" encoding="utf-8" ?> 
<Rules FriendlyName="CustomRules"> 
    <Rule TypeName="PublicFieldsMustBeReadonly" Category="CustomRules.ThreadSafety" CheckId="CF1001"> 
    <Name>Public Fields must be readonly or must be replaced with a Getter/Setter Method.</Name> 
    <Description>Public Fields must be readonly or must be replaced with a Getter/Setter Method.</Description> 
    <GroupOwner>MyCompany</GroupOwner> 
    <DevOwner>Me</DevOwner> 
    <Owner>Me</Owner> 
    <Url>http://example.com</Url> 
    <Resolution>The public Public Field "{0}" must be preceded of readonly</Resolution> 
    <Email>[email protected]</Email> 
    <MessageLevel Certainty="100">Warning</MessageLevel> 
    <FixCategories>Breaking</FixCategories> 
    </Rule> 
</Rules> 

這裏是Microsoft規則的XML規則聲明。

<Rules FriendlyName="Performance Rules"> 
    <Rule TypeName="RemoveUnusedLocals" Category="Microsoft.Performance" CheckId="CA1804"> 
    <Name> 
     Remove unused locals 
    </Name> 
    <Description> 
     Remove locals that are not used or are only assigned to in method implementations. 
    </Description> 
    <Url> 
     @ms182278(VS.100).aspx 
    </Url> 
    <Resolution> 
     {0} declares a variable, {1}, of type {2}, which is never used or is only assigned to. Use this variable or remove it. 
    </Resolution> 
    <Email /> 
    <MessageLevel Certainty="95"> 
     Warning 
    </MessageLevel> 
    <FixCategories> 
     NonBreaking 
    </FixCategories> 
    <Owner /> 
    </Rule> 
</Rules> 

回答

6

這是因爲規則的名稱(相對於它的類類型名稱)不通過代碼分析運行產生的FxCop報告違章消息包含。理論上,Visual Studio可能(也可能應該)在報告中的<Rules>節點下查找規則名稱,然後開始查看別處。但是,它不在VS 2012中。相反,它只從通過加載的規則程序集獲取的規則信息中獲取規則名稱。

這裏棘手的是,爲了UI顯示的目的,Visual Studio正在做一個完全獨立的搜索和加載規則程序集,而不是在代碼分析中運行。後者是通過運行fxcopcmd.exe並使用MSBuild屬性生成的命令行來執行的。前者使用嵌入Visual Studio插件中的完全獨立的邏輯進行代碼分析,並且不考慮項目級規則位置或規則集文件。 (這似乎是通過Microsoft.VisualStudio.CodeAnalysis.ManagedRuleProvider類完成的CodeAnalysis.dll組件,如果你好奇的血淋淋的細節。)

所以......你基本上有兩種選擇,如果你想看到的規則名稱:

  1. 將您的規則程序集放在<VS install directory>\Team Tools\Static Analysis Tools\FxCop\Rules文件夾中,以便它被Visual Studio拾取。

  2. 與價值添加HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0\Code Analysis\FxCopRulePath註冊表節點(創建一個名爲Code Analysis新的密鑰,用一個空格,而不是使用現有CodeAnalysis)像下面同時允許你的規則和內置的規則,讓他們的名字顯示:%FxCopDir%\Rules;C:\Foo\Bar\YourRulesFolder

+0

在我的情況,我用了'.ruleset'文件的'RuleHintPaths'標籤給的路徑,我的DLL,但正如你所說VS的插件似乎忽略它。謝謝你的回答,即使它不是我想看的:)。 –

+0

將文件複製到' \ Team Tools \靜態分析工具\ FxCop \規則「,但它不是最佳的,因爲我們經常添加或調整規則。註冊表項在我的系統中不存在,所以我嘗試創建它,但它似乎不工作。我有一個Windows 7 64位系統,鑰匙可以放在別的地方嗎?謝謝! –

+0

您是否在添加註冊表項後重新啓動Visual Studio?另外,您是否嘗試在預先存在的HKEY_CURRENT_USER \ Software \ Microsoft \ VisualStudio \ 11.0 \ CodeAnalysis'下添加FxCopRulePath值,或者是否創建了一個新的「Code Analysis」鍵,並在單詞之間留有空格? (後者是必要的。) –