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
| public override void Initialize(AnalysisContext context) | |
| { | |
| context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType); | |
| } |
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
| var classSymbol = context.Symbol as INamedTypeSymbol; | |
| if (classSymbol?.TypeKind != TypeKind.Class) | |
| return; //we only need classes, not other types like enum, struct, etc. |
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
| using System; | |
| namespace ConsoleApp1 | |
| { | |
| internal class Program | |
| { | |
| public static void Main(string[] args) | |
| { | |
| Console.WriteLine("Hello World!"); | |
| } |
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
| var members = new List<ISymbol>(); | |
| foreach (var member in classSymbol.GetMembers()) | |
| { | |
| if (member.Kind == SymbolKind.Method) | |
| { | |
| var methodMember = member as IMethodSymbol; | |
| if (methodMember.MethodKind == MethodKind.PropertyGet || methodMember.MethodKind == MethodKind.PropertySet) | |
| continue; | |
| } | |
| members.Add(member); |
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 ISymbol GetUnorderedMember(List<ISymbol> members) | |
| { | |
| //I'm just sorting the members by DeclaredAccessibility, then comparing them with the order of | |
| //the initial members | |
| var orderedByAccessibility = members.OrderByDescending(m => m.DeclaredAccessibility).ToList(); | |
| for (var i = 0; i < members.Count; i++) | |
| { | |
| if (members[i].DeclaredAccessibility != orderedByAccessibility[i].DeclaredAccessibility) | |
| { | |
| if (orderedByAccessibility[i].DeclaredAccessibility < members[i].DeclaredAccessibility) |
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 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 |
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
| using Microsoft.CodeAnalysis.Diagnostics; | |
| using System.Collections.Immutable; | |
| using System; | |
| using Microsoft.CodeAnalysis; | |
| using Microsoft.CodeAnalysis.Text; | |
| using System.Collections.Generic; | |
| using Microsoft.CodeAnalysis.CSharp; | |
| using System.Linq; |
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
| using Microsoft.CodeAnalysis; | |
| using Microsoft.CodeAnalysis.Text; | |
| using Microsoft.CodeAnalysis.CSharp; | |
| using Microsoft.CodeAnalysis.Diagnostics; | |
| using System; | |
| using System.Linq; | |
| using System.Collections.Generic; | |
| using System.Collections.Immutable; |
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
| using Microsoft.CodeAnalysis; | |
| using Microsoft.CodeAnalysis.Diagnostics; | |
| using Microsoft.CodeAnalysis.CSharp.Syntax; | |
| using System.Linq; | |
| using System.Collections.Immutable; | |
| namespace MyCodeAnalyzers | |
| { | |
| [DiagnosticAnalyzer(LanguageNames.CSharp)] |
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
| using Microsoft.CodeAnalysis; | |
| using Microsoft.CodeAnalysis.CSharp; | |
| using Microsoft.CodeAnalysis.Rename; | |
| using Microsoft.CodeAnalysis.Editing; | |
| using Microsoft.CodeAnalysis.CodeFixes; | |
| using Microsoft.CodeAnalysis.CodeActions; | |
| using Microsoft.CodeAnalysis.CSharp.Syntax; | |
| using System; | |
| using System.Linq; |