Wednesday, 15 May 2013

c# - How can I retrieve a nested type with its fully qualified name? -



c# - How can I retrieve a nested type with its fully qualified name? -

i cannot seem retrieve nested class using roslyn's compilation.gettypebymetadataname() method.

for example:

var tree = csharpsyntaxtree.parsetext(@" using system; namespace mynamespace { public class myclass { public class myinnerclass { } } } "); var mscorlib = new metadatafilereference(typeof(object).assembly.location); var compilation = csharpcompilation.create("mycompilation", syntaxtrees: new[] { tree }, references: new[] { mscorlib }); //correctly retrieves outer type. var outerclass = compilation.gettypebymetadataname("mynamespace.myclass"); //cannot correctly retrieve inner type (returns null) var innerclass = compilation.gettypebymetadataname("mynamespace.myclass.myinnerclass");

is possible retrieve nested types using qualified names?

i realize 1 work around first check if containing type contained types using inamespaceortypesymbol.gettypemembers(), much rather not go downwards path. i'd assume gettypebymetadataname() method should work type, nested or otherwise.

try using + separate inner classes:

var innerclass = compilation.gettypebymetadataname("mynamespace.myclass+myinnerclass");

the documentation type.gettype method discusses syntax used naming nested types.

c# nested roslyn

No comments:

Post a Comment