Category Archives: StyleCop

SA1650: The documentation text within the summary tag contains incorrectly spelled words – Change StyleCop default language to British English

在填寫 Documentation Header
StyleCop 出現了以下的Warning
The documentation text within the summary tag contains incorrectly spelled words: initialiseSA1650 The documentation text within the summary tag contains incorrectly spelled words: initialise

Initialise 是沒有寫錯的..
所以相信這應該是 StyleCop 設定上字典的語言 問題
E.G. 應該是使用了 en-us 美式英語.. 而我用的是英式英語 en-gb
美式英語是這樣寫的 “Initialize
而英式卻時這樣 “Initialise

為了避免語言寫法問題再次出現warning解決方法是嘗試更改StyleCop的預設語言 的值

解決方法:

原來只有在Project上按 “滑鼠右鍵/ Mouse Right Click
之後在選單上選擇 “StyleCop Settings
StyleCop Settings Options

在”Options Tab / 選項分頁” 上的 “Culture for analysis” 上選擇 “en-GB” 後按 “OK / 確定“便可
StyleCop Change Default Language

再次執行 StyleCop 之後 Warning SA1650 便消失了

Hope you find it useful

SA1600 : CSharp.Documentation : The class must have a documentation header.

今天遇到的 StyleCop Warning是這一個
SA1600 : CSharp.Documentation : The method must have a documentation header.
SA1600 : CSharp.Documentation : The method must have a documentation header.

當剛剛建立完一個function 之後便會出現..
原因是因為沒有 documentation header 去解釋這一個功能有什麼作用
和他的變數代表什麼
還有會return出來的是什麼等等

public string SayHello(string yourName)
        {
            return "Hello " + yourName;
        }

解決方法十分簡單
只是填上 這個功能的SummaryParameter 是什麼東西 和 Return什麼便可以

解決方法:
E.G.

        /// <summary>
        /// This function is use to say hello to the input [yourName]
        /// </summary>
        /// <param name="yourName">The name to say Hello to</param>
        /// <returns>return "Hello the input name"</returns>
        public string SayHello(string yourName)
        {
            return "Hello " + yourName;
        }

SA1600 : CSharp.Documentation : The method must have a documentation header. (Solved)
Hope you find it useful

Edit StyleCop Settings – StyleCop – SA1126 – Disable some of the StyleCop Rules – 停用某些StyleCop的規則

今天繼續嘗試把 ASP.Net MVCProject 的Code 轉成 乎合 StyleCopResharperCoding Standard的 程式碼…
誰不知遇到一個 StyleCop 提出 警告

SA1126 : CSharp.Readability : The call to Session must begin with the ‘this.’, ‘base.’, ‘object.’ or ‘ContactController.’ or ‘Controller.’ prefix to indicate the intended method call.

SA1126 : CSharp.Readability : The call to View must begin with the ‘this.’, ‘base.’, ‘object.’ or ‘ContactController.’ or ‘Controller.’ prefix to indicate the intended method call.

SA1126 : CSharp.Readability : The call to View must begin with the 'this.', 'base.'
我的程式碼是這樣的:

 public ActionResult Index()
        {
            if (Session["UserInfo"] == null || Session["UserInfo"].ToString() != contactInfo.Name)
            {

                return View("Error");
            }
            return View();
        }

當我把程式碼上的ViewSession 前加上了 “this.
StyleCop的警告便消失了

但是 Resharper卻出現了警告信息:
Qualifier ‘this.’ is redundant
Qualifier This Is Redundant

在這個情況下..Resharper有他的道理…
所以我便希望改變StyleCop的規定去適合自己的喜好 和適合Resharper的設定了

嘗試找StyleCop 的設定很久很久..終於找到了..

解決方法:
原來只有在Project上按 “滑鼠右鍵/ Mouse Right Click
之後在選單上選擇 “StyleCop Settings
StyleCop Settings Options

之後便會看到 “StyleCop Project Settings” 的視窗
由於這些規則是以屬性類型來分類的..
所以十分方便
StyleCop Project Settings

以我的例子:
警告是和CSharp.Readability 相關的
我便可以按一下ReadabilityMember Access相關的 開啟了相關的 Folder便 看到了 SA1126
只要把他untick 便可以停用他了
完成後按一下”確定/OK” 便可以了
StyleCop Project Settings Disable - SA1126

再次執行StyleCop這個警告便不見了

*由於這個設定是以Project為單位的.. 你可能要幫每一個Project都要這樣停用某些規則才能解決..
有時間的話..希望可以和大家分享…如何更變StyleCop的設定 令到只需要更變一個設定檔便可以解決所有Project的問題

Hope you find it useful

SA1400 : CSharp.Maintainability : The class must have an access modifier.

今天另一個要處理的StyleCop 警告是
SA1400 : CSharp.Maintainability : The class must have an access modifier.

SA1400 : CSharp.Maintainability : The class must have an access modifier.

 

原來在原本的Class Template 是沒有定義 Access Modifier的

SA1400 : CSharp.Maintainability : The class must have an access modifier.

<strong>class Get_Text_CL</strong>
    {
    }

解決方法
只要在Class的開頭加上適當的 Access Modifer 便可以了
E.G. Public, Protected, Private..等等

E.G.

<strong>public class Get_Text_CL</strong>
    {
    }

終於解決了 所有的警告..
StyleCop All Fixed
Happy Coding

SA1642 : CSharp.Documentation : The documentation text within the constructor’s summary tag must begin with the text: Initializes a new instance of the class.

今天繼續嘗試把正在開始開發的Windows Phone 8 App的程式碼符合StyleCop的規定
當我使用StyleCop 檢查我的程式碼時出現了很多警告
E.G.

SA1642 : CSharp.Documentation : The documentation text within the constructor’s summary tag must begin with the text: Initializes a new instance of the class.

以下是我寫在Constructor上寫的Comment

/// <summary>
       /// <strong>This is the constructor of this class</strong>
       /// </summary>
        public MainPage()
        {
            InitializeComponent();

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

誰不知..Style Cop 的規定是要在Constructor 上寫以以下的寫句開頭…
Initializes a new instance of the class.”

/// <summary>
        /// <strong>Initializes a new instance of the <see cref="</strong>MainPage<strong>" /> class</strong>
        /// and predefine variable settings and check ...etc
       /// </summary>
        public MainPage()
        {
            InitializeComponent();

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

Happy Coding

SA1633 : CSharp.Documentation : The file has no header, the header Xml is invalid, or the header is not located at the top of the file.

SA1633 : CSharp.Documentation : The file has no header, the header Xml is invalid, or the header is not located at the top of the file.”
SA1633 : CSharp.Documentation : The file has no header, the header Xml is invalid, or the header is not located at the top of the file.

StyleCop 有些守則是要每一個文件上的開首
都要有一段XML碼去簡單說明這個程式碼或文件是用來做什麼..
版權是誰的等等

//-----------------------------------------------------------------------
// <copyright file="Get_Text_CL.cs" company="LookFor.HK">
//     Copyright LookFor.HK. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

SA1633 : CSharp.Documentation : The file has no header, the header Xml is invalid, or the header is not located at the top of the file.

Happy Coding

SA1623 : CSharp.Documentation : The property’s documentation summary text must begin with: Gets or sets

今天遇到的另一個StyleCop 警告是
SA1623 : CSharp.Documentation : The property’s documentation summary text must begin with: Gets or sets
SA1623 : CSharp.Documentation : The property's documentation summary text must begin with: Gets or sets

        /// <summary>
        /// <strong>Date Of Entry</strong>
        /// </summary>
        public string DOE { get; set; }

解決方法
在 [Summary] 是要使用 “Gets or sets” 來開首的.. 即使大小寫不同也會出現上面的警告信息
E.G.

        /// <summary>
        ///<strong> Gets or sets Date Of Entry</strong>
        /// </summary>
        public string DOE { get; set; }

Hope you find it useful