How I could derive SyntaxNode?

Topics: APIs
May 22, 2014 at 2:04 PM
Edited May 22, 2014 at 2:09 PM
Hello! I want do derive SyntaxNode to use it in typed translation Stack<SyntaxNode> for custom actions during syntax tree traversal. But I can't derive SyntaxNode because it has internal constructor. I definitely do not want to rebuild Roslyn from sources or lose strong typing in my stack, so is there any class derived from SyntaxNode which I could derive?

May 22, 2014 at 5:59 PM
Edited May 22, 2014 at 6:00 PM
PavelBNovikov wrote:
is there any class derived from SyntaxNode which I could derive?
That's an easy question to answer with a bit of reflection and LINQ:
public static IEnumerable<Type> GetAncestorTypes(Type type)
    while (type.BaseType != null)
        yield return type.BaseType;
        type = type.BaseType;

public static IEnumerable<Type> GetAllDerivedTypesInAssembly(Type baseType)
    return baseType.Assembly.GetTypes().Where(t => GetAncestorTypes(t).Contains(baseType));


from type in GetAllDerivedTypesInAssembly(typeof(CSharpSyntaxNode))
where type.GetConstructors().Any() // parameterless GetConstructors returns only public constructors
select type;
This query doesn't return anything, so it seems like the answer is "no".
May 22, 2014 at 7:50 PM
Thanks a lot! :)

I've thought a little bit and found pretty and simple workaround to deal with it.