建立標準編碼規則-自訂C#程式碼分析器

來源:互聯網
上載者:User

標籤:nload   replace   測試   分享   rod   公司   which   compute   category   

1.下載Roslyn的Visual Studio分析器模板外掛程式(VS2015 或VS2017)https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.NETCompilerPlatformSDK我後來查詢到官方說明vs2017已經內嵌了此功能Want to start developing in C# and Visual Basic? Download Visual Studio 2017, which has the latest features built-in. There are also prebuilt Azure VM images available with Visual Studio 2017 already installed.Roslyn(https://github.com/dotnet/roslyn)  2.建立分析器開啟visual studio 2017 選擇Extensibility中的

 

3.測試分析器系統預設的分析器是一個檢測類名不能使用小寫規則
 private static void AnalyzeSymbol(SymbolAnalysisContext context)        {            // TODO: Replace the following code with your own analysis, generating Diagnostic objects for any issues you find            var namedTypeSymbol = (INamedTypeSymbol)context.Symbol;            // Find just those named type symbols with names containing lowercase letters.            if (namedTypeSymbol.Name.ToCharArray().Any(char.IsLower))            {                // For all such symbols, produce a diagnostic.                var diagnostic = Diagnostic.Create(Rule, namedTypeSymbol.Locations[0], namedTypeSymbol.Name);                context.ReportDiagnostic(diagnostic);            }        }
運行F5,系統回建立一個vs副本視窗,我們建立一個consoleapp應用程式,

我們可以在分析器項目中設定斷點,查看具體的運行效果,如我手動改Program為Program2,分析器就會偵測到改動,運行分析器代碼,具體斷點效果如下:

 

總結:官方已經提供了一個完整的機遇Roslyn的程式碼分析器自訂模板;我們需要建立基於此模板的分析器程式就可以了;我們可以根據公司的項目規定按指定的規則設計程式碼分析器規則;

 

 參考:C# and Visual Basic - Use Roslyn to Write a Live Code Analyzer for Your API

 

建立標準編碼規則-自訂C#程式碼分析器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.