We are trying to use the Roslyn APIs to analyse an existing application with a view to be able to write tools to asses it for quality.
In the code below we are trying to use the APIs to get the IOperation
for a particular method being called via async/await call:
if (toEvaluate is AwaitExpressionSyntax awaitExpressionSyntax)
{
if (awaitExpressionSyntax.Expression is InvocationExpressionSyntax invocationExpressionSyntax)
{
var operation = semanticModel.GetOperation(invocationExpressionSyntax);
// Analyse the operation
}
}
We are seeing this work in most circumstances but for one particular method call GetOperation
is return an IOperation
with a Kind
of OperationKind.Invalid
.
Since we are analysing code from a working application we know the code itself isn’t invalid.
What can cause GetOperation
to return OperationKind.Invalid
?
Is there any debugging information that can be used to understand what the operation is deemed invalid?
In the end we were able to get a bit of a clue as to what the problem was by looking at the output from semanticModel.GetDiagnostics()
which indicated that the model was missing a reference to netstandard.dll
.
Adding the missing DLL does seem to have fixed the problem.
Its not entirely clear why this was working for other methods as even for methods where semanticModel.GetOperation(invocationExpressionSyntax)
returned successfully (i.e. OperationKind
wasn’t Invalid
) the errors in the diagnostics were still there.