I am following this tutorial, https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/tutorials/how-to-write-csharp-analyzer-code-fix
What I really want is to detect if a method in a ASP.Net Web API controller is missing my Custom
attribute and give hints to the developer to add it.
In my Analyzer's Initilize method, I have chosen MethodDeclaration
as the SyntaxKind
like this
context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.MethodDeclaration);
In the AnalyzeNode method, I want to detect if the method in question already has the Custom
attribute added to it.
private void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
var methodDeclaration = (MethodDeclarationSyntax)context.Node;
// make sure the declaration isn't already const:
if (methodDeclaration.AttributeLists.Any(x=> x. ))
{
return;
}
Not sure what needs to be done in this piece of code to find if Custom
attribute is already applied.
Eventually I want my code analyzer to let the user add the missing attribute
[Route("/routex")]
[Custom()]
public async Task<IHttpActionResult> AlreadyHasCustomAttribute()
{
//everything is good, no hint shown to the user
}
[Route("/routey")]
public async Task<IHttpActionResult> DoesNotHaveCustomAttribute()
{
//missing Custom attribute, show hint to the user and add the attribute as a code fix
}
Please suggest a solution. Thanks.