Pavel Voronin
Pavel Voronin

Reputation: 13983

How to get type from compilation mode when it is defined in extern alias assembly?

Compilation model has method Compilation.GetTypeByMetadataName

with the following description:

Gets the type within the compilation's assembly and all referenced assemblies (other than those that can only be referenced via an extern alias) using its canonical CLR metadata name.

But how do I get the type from extern alias assembly?

From docs it follows, that I need to know alias namespace name, but I want to compare ITypeSymbol with a known type. It's weird I cannot specify type and original assembly name.

Update:

After pondering a bit, I came to conclusion that it is impossible. I can reference two identically named assemblies located in separate folders. So again, I need to know their alias names to properly specify type name.

Upvotes: 1

Views: 59

Answers (1)

Jason Malinowski
Jason Malinowski

Reputation: 19021

So you can do this sequence of operations:

  1. Look through Compilation.References, and find the reference that has the alias you're looking for.
  2. Call Compilation.GetAssemblyOrModuleSymbol passing in the reference to get its assembly symbol.
  3. Look at the IAssemblySymbol.GlobalNamespace, and manually walk the namespace from there.

And if you need to go in the reverse direction, there's Compilation.GetMetadataReference which takes the assembly symbol.

Upvotes: 1

Related Questions