I'm making a roslyn demo for generating compiler warnings from attributes
I have an analyzer to analyze Method Invocations which looks like so:
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(AnalyzerInvocation, SyntaxKind.InvocationExpression);
}
private static void AnalyzerInvocation(SyntaxNodeAnalysisContext context)
{
var invocation = (InvocationExpressionSyntax)context.Node;
}
I'm trying to figure out how to get the method declaration, I know I can use the SymbolFinder
to search for the method declaration
var model = compilation.GetSemanticModel(tree);
//Looking at the first method symbol
var methodSyntax = tree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>()
.First(/*TODO: Execute Find for related symbol */);
This options is expensive and annoying, and it leaves open the possiblity for error because what if your invoking method is coming from an assembly.
What is the easiest way to get the method declaration from an InvocationExpressionSyntax? Should I just be using the symbol finder and if it fails use scour the imported assemblies or is there an easier better way?