I'm writing a code analyzer with Roslyn, and I need to check if an ExpressionSyntax
is of type Task
or Task<T>
.
So far I have this:
private static bool IsTask(ExpressionSyntax expression, SyntaxNodeAnalysisContext context)
{
var type = context.SemanticModel.GetTypeInfo(expression).Type;
if (type == null)
return false;
if (type.Equals(context.SemanticModel.Compilation.GetTypeByMetadataName("System.Threading.Tasks.Task")))
return true;
if (type.Equals(context.SemanticModel.Compilation.GetTypeByMetadataName("System.Threading.Tasks.Task`1")))
return true;
return false;
}
It works for Task
, but not for Task<int>
or Task<string>
... I could check the name and namespace, but it's impractical because I have to check each "level" of the namespace.
Is there a recommended way to do it?