Created
April 3, 2018 23:46
-
-
Save bogdanbujdea/d3acc2c31ea10bbe1a75fccfefe68edc to your computer and use it in GitHub Desktop.
Reporting an error from Roslyn analyzer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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