Skip to content

Instantly share code, notes, and snippets.

@bogdanbujdea
Created April 3, 2018 23:46
Show Gist options
  • Select an option

  • Save bogdanbujdea/d3acc2c31ea10bbe1a75fccfefe68edc to your computer and use it in GitHub Desktop.

Select an option

Save bogdanbujdea/d3acc2c31ea10bbe1a75fccfefe68edc to your computer and use it in GitHub Desktop.
Reporting an error from Roslyn analyzer
private static void AnalyzeSymbol(SymbolAnalysisContext context)
{
var classSymbol = context.Symbol as INamedTypeSymbol;
if (classSymbol?.TypeKind != TypeKind.Class)
return; //we only need classes, not other types like enum, struct, etc.
var members = GetMembersFromClass(classSymbol); //retrieve only methods and properties
var unorderedMember = GetUnorderedMember(members); //then find the first member that is out of order
if (unorderedMember != null)
{
var rule = new DiagnosticDescriptor(DiagnosticId, Title, $"Move {unorderedMember.Name} lower in the file", Category,
DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
//now we're creating a rule that tells the user to move the member lower in the file
var diagnostic = Diagnostic.Create(rule, unorderedMember.Locations[0], classSymbol);
context.ReportDiagnostic(diagnostic);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment