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