I am writing a Roslyn analyzer to raise diagnostics when a specific library method is used in a specific method of a particular class, but I cannot get a character in the parent or ancestral syntax nodes.
For example,
class C
{
void M()
{
MyLibrary.SomeMethod();
}
}
And this is the code for analysis SyntaxNode SyntaxKind.InvocationExpression
private void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
var invocationExpression = context.Node as InvocationExpressionSyntax;
var methodSymbol = context.SemanticModel.GetSymbolInfo(invocationExpression).Symbol as IMethodSymbol;
if (methodSymbol == null) { return; }
if (!methodSymbol.Name.Equals("SomeMethod") ||
!methodSymbol.ContainingSymbol.ToString().Equals("MyNamespace.MyLibrary"))
{ return; }
var outerMethodSyntax = invocationExpression.FirstAncestorOrSelf<MethodDeclarationSyntax>();
if (outerMethodSyntax == null) { return; }
var symbol = context.SemanticModel.GetSymbolInfo(outerMethodSyntax);
...
So my question is: is it possible to return SymbolInfofrom the ancestor SyntaxNode.
Is my approach right or should I try a different approach?
source
share