I have a CSharpCompilation
instance containing an array of SyntaxTree
s and I am trying to find all the class declarations that inherit from a class
So I came up with the following which will recursively check all classes for the inherited type
public class BaseClassRewriter : CSharpSyntaxRewriter
{
private readonly SemanticModel _model;
public BaseClassRewriter(SemanticModel model)
{
_model = model;
}
public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
{
var symbol = _model.GetDeclaredSymbol(node);
if (InheritsFrom(symbol))
{
// hit!
}
}
private bool InheritsFrom(INamedTypeSymbol symbol)
{
while (true)
{
if (symbol.ToString() == typeof(T).FullName)
{
return true;
}
if (symbol.BaseType != null)
{
symbol = symbol.BaseType;
continue;
}
break;
}
return false;
}
}