You can do this with the Roslyn Code Analyzer. The following code will create a DiagnosticAnalyzer , which will give a warning to the compiler if String.EndsWith() .
[DiagnosticAnalyzer(LanguageNames.CSharp)] public class ForbiddenMethodsAnalyzer : DiagnosticAnalyzer { private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor("Forbidden", "Don't use this method!", "Use of the '{0}' method is not allowed", "Forbidden.Stuff", DiagnosticSeverity.Warning, isEnabledByDefault: true, description: "This method is forbidden"); public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } } public override void Initialize(AnalysisContext context) { context.RegisterSyntaxNodeAction(AnalyzeSyntaxNode, SyntaxKind.InvocationExpression); } private static void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context) { var invocationExpression = (InvocationExpressionSyntax)context.Node; var memberAccessExpression = invocationExpression.Expression as MemberAccessExpressionSyntax; if (memberAccessExpression?.Name.ToString() == "EndsWith") { var memberSymbol = context.SemanticModel.GetSymbolInfo(memberAccessExpression).Symbol as IMethodSymbol; var containingType = memberSymbol.ContainingType; if (containingType.ContainingNamespace.Name == "System" && containingType.Name == "String") { var diagnostic = Diagnostic.Create(Rule, invocationExpression.GetLocation(), memberAccessExpression.ToString()); context.ReportDiagnostic(diagnostic); } } } }

There are 3 options for using the analyzer:
- Add the
DiagnosticAnalyzer code directly to your project. It will only apply to this decision. - Create a class library with
DiagnosticAnalyzer in it and distribute it as a Nuget package. It will only apply to solutions using the package. - Compile the full VSIX extension containing the class. The analyzer will work with any solution you download.
This is the first project I have used that uses the Roslyn code analysis functionality, so unfortunately I do not understand everything that happens here. I started with the default analyzer template and tried various methods, went through the code and looked at the variables with the clock windows until I found the information needed for this function.
The main process is to register the SyntaxNode Analysis function, filtered in expressions that invoke the method. Inside this method, I check to see if the Name the MemberAccessExpressionSyntax "EndsWith" is MemberAccessExpressionSyntax checked. If so, I get a ContainingType that this method is included, and check if it is in the String class in the System namespace. If so, I create an instance of Diagnostic from the DiagnosticDescriptor to tell the IDE where the problem is and what part of the problem it represents (warning in this case, I could make it a complete mistake if I wanted to, which would prevent code compilation ) It is also possible to present the user with various parameters in order to automatically correct the error, but I have not yet studied it.
A lot of information from this tutorial , as well as a lot of trial and error.
source share