How to do it...

  1. Add a new project to your Visual Studio solution and click on Extensibility and select the Analyzer with Code Fix (NuGet + VSIX) template. Give it a suitable name and click on OK to create the Analyzer project.
  1. You will see that Visual Studio has created three projects for you: Portable, .Test and .Vsix . Ensure that the .Vsix project is set as the default startup project.
  1. In the Portable class, take a look at the DiagnosticAnalyzer.cs file. You will see a method called AnalyzeSymbol(). All that this code analyzer does is simply check for the existence of lowercase letters on the namedTypeSymbol variable.
        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);
}
}
  1. Build your project and click on F5 to start debugging. This will start a new instance of Visual Studio with its own settings. This means anything you change in this experimental instance of Visual Studio will not affect your current Visual Studio installation. You can open an existing project or create a new one. I simply created a console application. From the start, you will see that the Program class name is underlined. Hovering your cursor over this will display the Visual Studio lightbulb and tell you that the type name contains lowercase letters.
  1. Clicking on Ctrl + . or on the Show potential fixes link in the tooltip will display the fixes you can apply to correct the error.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.219.130