Establish standard coding Rules-Customize C # code Analyzer

Source: Internet
Author: User

1. Download the Visual Studio Parser template plug-in for Roslyn (VS2015 or VS2017)Https://marketplace.visualstudio.com/items?itemName= Visualstudioproductteam.netcompilerplatformsdk I later queried the official note vs2017 already embedded this feature want to start developing in C # and Visual Basic? Download Visual Studio, which has the latest features built-in. There is also prebuilt Azure VM images available with Visual Studio already installed. Roslyn (Https://github.com/dotnet/roslyn) 2. Creating an analyzerOpen Visual Studio 2017 in the Select extensibility

3. Test AnalyzerThe system default parser is a rule that detects that a class name cannot use lowercase
 Private Static voidAnalyzesymbol (Symbolanalysiscontext context) {//Todo:replace The following code with your own analysis, generating Diagnostic objects for all issues you find            varNamedtypesymbol =(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.                varDiagnostic = Diagnostic.create (Rule, namedtypesymbol.locations[0], namedtypesymbol.name); Context.            Reportdiagnostic (diagnostic); }        }
Running F5, the system returns a new VS replica window, we create a new ConsoleApp application,

We can set breakpoints in the parser project to see the specific effect of the operation, such as I manually change program to PROGRAM2, the parser will detect changes, run the parser code, the specific breakpoint effect is as follows:

Summary:The official has provided a complete opportunity for the Roslyn Code Parser custom template; We need to create a new parser based on this template; we can design code parser rules According to the company's project rules;

Reference: C # and Visual Basic-use Roslyn to Write a Live Code Analyzer for Your API

Establish standard coding Rules-Customize C # code Analyzer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.