2010-09-01 35 views
3

我得到的DoNotPassLiteralsAsLocalizedParameters的FxCop侵犯兩個例外在下面的方法代碼投擲線:排除的FxCop DoNotPassLiteralsAsLocalizedParameters違法行爲異常實例化或本地化異常消息

public bool IsPageAccessible(string url, string documentId) { 
    if (url == null) { 
     throw new ArgumentNullException("url", @"url must not be null, use string.Empty if you don't care what the url is."); 
    } 

    if (documentId == null) { 
     throw new ArgumentNullException("documentId", "documentId must not be null, use string.Empty if you don't care what the documentId is."); 
    } 
    return true; 
} 

這意味着:

的FxCop全球化# CA1303字符串 嵌入源 代碼中的文字難以本地化。避免 在通常需要本地化的 字符串的情況下將字符串文字作爲參數 傳遞。大多數 本地化應用程序,例如, 應本地化字符串參數是 傳遞給異常的構造函數。 當創建一個異常實例, 因此,字符串參數檢索 從一個字符串表比字符串文字更 合適。

推理:

我不想本地化異常消息。只有英文很好。儘管我們正在構建一個API,但所有開發人員都知道英語。不管怎樣,不應該在生產服務器上向訪問者顯示異常消息。

問題:

  • 你與我的異常消息本地化推理不同意?爲什麼?
  • 有沒有辦法從所有異常實例化中排除這個FxCop警告?我們確實本地化API的其他部分。這些部分將對最終用戶具有可見的文本。所以我們從這些情況下保留警告的價值。
  • 您認爲我應該如何處理這個問題?

回答

1

我覺得你的推理很好,我討厭它,當我在Visual Studio中有本地化的異常,並且因爲編程的通用語言是英語而無法找到幫助。

更一般地,你不應該試圖迎合每一個的FxCop規則,這樣可以迅速成爲一種負擔。最好專注於一部分規則。

我不認爲你可以在一個特定的異常排除警告,但可以使用SuppressMessage屬性排除檢測:

[SuppressMessage("Microsoft.Globalization", 
       "CA1303:DoNotPassLiteralsAsLocalizedParameters", 
       Justification="Exception are not localized")] 
public bool IsPageAccessible(string url, string documentId) { 
    if (url == null) { 
    throw new ArgumentNullException("url", @"url must not be null, use string.Empty if you don't care what the url is."); 
    } 

    if (documentId == null) { 
    throw new ArgumentNullException("documentId", "documentId must not be null, use string.Empty if you don't care what the documentId is."); 
    } 
    return true; 
} 

另一種方式,是寫一個custom fxcop rule添加此行爲。