What I'd like to achieve
I'd like to write a code analyzer that picks up on any ObjectCreationExpression
such as:
FruitMix fm = new FruitMix();
and allow me to find out what interfaces that type implements, performing an action if a particular interface is found.
What I've tried
I've intercepted the analysis through registering the SyntaxNodeAction via:
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(c=> AnalyzeObjectCreation(c), SyntaxKind.ObjectCreationExpression);
}
in order to get the interfaces ITypeSymbol seems to be the way to go which I'm attempting to obtain in the registered method:
private static void AnalyzeObjectCreation(SyntaxNodeAnalysisContext context)
{
ObjectCreationExpressionSyntax objectCreation = (ObjectCreationExpressionSyntax) context.Node;
//How do I get an INamedTypeSymbol here?
//INamedTypeSymbol typeSyntax = (INamedTypeSymbol)objectCreation.Type;
//ISymbol test = typeSyntax.AssociatedSymbol;
//ISymbol test = context.SemanticModel.GetDeclaredSymbol(context.Node);
//SymbolInfo symbolInfo = context.SemanticModel.GetDeclaredSymbol()
}
As you can see I've tried everything I can find online to no avail. Perhaps I shouldn't be looking for an INamedTypeSymbol at all - if so what do I need?
Could you point me in the right direction?