1

I need to search the referenced assemblies of the compilation under analysis for types that have the classname specified, irrespective of the namespace it is within.

Essentially I need a function that searches the referenced assemblies in the same way the below method searches the compilation under analysis.

context.SemanticModel.Compilation.GetSymbolsWithName(classNameToFind, SymbolFilter.Type);

Is there a way to load a compilation abstraction using the assembly name? I am ok with crude ways as long as I do not have to use reflection....so that I can continue to work with ISymbol.

Kilo7
  • 31
  • 3
  • I also tried to use context.SemanticModel.Compilation.SourceModule.ReferencedAssemblySymbols.SelectMany(x => x.TypeNames) but the typenames are not fully qualified. – Kilo7 Nov 01 '21 at 17:38

1 Answers1

1

I don't think we have a special API for this; you can just go to the Compilation's GlobalNamespace and manually walk the namespace/type hierarchy.

Jason Malinowski
  • 18,148
  • 1
  • 38
  • 55
  • Hello @Jason Malinowski. Thanks for your answer....but can you be a little more specific as I cannot find anything useful in there. The only mechanism I can think is to analyze the assemblies using reflection (I am mainly afraid of the manual loading of the dependent assemblies here), then find the fully qualified name of the type I need and then get the symbol using context.SemanticModel.Compilation.GetTypeByMetadataName(x). – Kilo7 Nov 02 '21 at 09:16
  • @Kilo7: once you get the global namespace you can call [GetNamespaceMembers](https://learn.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.inamespacesymbol.getnamespacemembers) recursively to get all the namespaces, then on each of those you can call [GetTypeMembers](https://learn.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.inamespaceortypesymbol.gettypemembers?view=roslyn-dotnet-3.11.0) to get the types that match that name. – Jason Malinowski Nov 05 '21 at 00:44
  • And you are correct to not use reflection: Roslyn has it's own model here you can use that avoids the headaches. – Jason Malinowski Nov 05 '21 at 00:47