Roslyn gets details in {SomeSyntax} .Type
I have a generic class:
public class NestedDict<T>: Dictionary<string, Dictionary<string, T> { }
As shown in the picture, I can parse and access it BaseList
and find out that it TypeArgumentList
is there in the debugger. However, I cannot just call baseType.Type.TypeArgumentList
to access it, because it is not a public member that I can access.
Generally, what is the correct way to access the TypeArgumentList
pod Type
in my case? or any data type in general?
source to share
While you may know that baseType
this is a generic type (So a GenericNameSyntax
), this is not the only possible case, which is why objects in BaseList
are of a parent type ( TypeSyntax
).
You only need to click to access TypeArgumentList
:
var tree = CSharpSyntaxTree.ParseText("public class NestedDict<T>: Dictionary<string, Dictionary<string, T> { } ");
var cu = (CompilationUnitSyntax)tree.GetRoot();
var c = (ClassDeclarationSyntax)cu.ChildNodes().Single();
var baseDeclaration = (BaseTypeSyntax)c.BaseList.ChildNodes().Single();
var baseNameSyntax = (GenericNameSyntax)baseDeclaration.Type;
Console.WriteLine(baseNameSyntax.TypeArgumentList.Arguments[0].ToFullString());
Console.WriteLine(baseNameSyntax.TypeArgumentList.Arguments[1].ToFullString());
source to share