How can I get a list of assemblies referenced from a given assembly?

# How can I get a list of assemblies referenced from a given assembly?

• 4 decembrie 2011 03:08

Is there a way to get all the assemblies referenced from a given assembly or module?

Thanks,
Chris

• Editat de 4 decembrie 2011 03:08
•

### Toate mesajele

• 4 decembrie 2011 18:09

Hi,

After you've compiled something or when you are doing an analysis?

Mark good replies as helpful and correct ones as answers! - http://blog.filipekberg.se
• 4 decembrie 2011 19:44

Filip, hi, given an AssemblySymbol.

Thanks,
Chris

• 5 decembrie 2011 22:05
Proprietar

Hi Chris - I don't think we currently have a way to go from an AssemblySymbol / ModuleSymbol to the AssemblySymbols / ModuleSymbols that it references.

If you know that the AssemblySymbol maps to a Compilation, then you can get at the AssemblySymbols for all the assemblies that are referenced by this Compilation using Compilation.GetReferencedAssemblySymbol() as shown below. But this is probably not what you are looking for. (For example, this wouldn't work if you wanted to find all the assemblies being referenced from a metadata assembly...)

var source = @"
class Program
{
static void Main()
{
}
}";

var tree = SyntaxTree.ParseCompilationUnit(source);
var mscorlib = new AssemblyFileReference(typeof(object).Assembly.Location);
var core = new AssemblyFileReference(typeof(System.Linq.Enumerable).Assembly.Location);
var compilation = Compilation.Create("MyCompilation",
syntaxTrees: new[] { tree },
references: new[] { mscorlib, core });

var assembly = compilation.Assembly;
foreach (var reference in compilation.References)
{
var referencedAssembly = compilation.GetReferencedAssemblySymbol(reference);
Console.WriteLine(referencedAssembly);
}

Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
• Marcat ca răspuns de 5 decembrie 2011 22:41
•
• 5 decembrie 2011 22:26

Shyam, thanks. Yeah, I want to enumerate the AssemblySymbol references -- not the compilation's references. Could you add it to your list of feature references? It appears that assembly references are an integral part of it's identity of AssemblySymbols so it'd be great to be able to enumerate those references. IMHO its a good idea to expose the components of an entities identity explicitly. Hiding the components will not prevent folks from breaking if the identity changes.

Did we ever discover when you can or cannot open something in Connect and link it back to the Forums? I'm not clear on when, if ever, you can do that... it would be encouraging for all us enthusiasts if you could be allowed to do that. Anyway...

Thanks,

Chris

• 5 decembrie 2011 22:58
Proprietar

Thanks Chris - I agree there would certainly be value in exposing the AssemblySymbol references. I shall log this suggestion and post back here with any updates (alternately please feel free to log this on Connect and cross-link with this post)...

Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
• 6 decembrie 2011 01:37

• 6 decembrie 2011 19:11
Proprietar

Awesome - Thank you Chris!
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
• 17 ianuarie 2012 00:03

Anthony,

Glad to hear the team is open to the idea of exposing the assembly references. Here is my pitch for the feature:

Being able to enumerate the resolved Assembly symbols for a given assembly is a natural thing to expose because the object identity of an assembly symbol is composed of it's location on disk and the list of assembly symbols it references. Users can take a dependency on assembly symbol identity today. Exposing the list makes the identity explicit and therefor less likely to be overlooked by users and the test team.

And, implementation is trivial as you already have the list hanging off of the manifest module. It's trivial because it's natural to expose because, again, it's part of the identity.

Exposing the string name of the assembly would be good too.

Thanks,
Chris

• 14 februarie 2012 21:35

Anthony,

The list of referenced assemblies should include all thosed used in establishing the identity of the assembly. Specifically, it should include AssemblySymbols for custom attributes which are referenced by FQN in the CA blob.

I'm trying to resolve FQN against an AssemblySymbol and I want to return null if the FQN's assembly is not referenced in anyway by the AssemblySymbol.

Thanks,
Chris

• 14 februarie 2012 22:02

If I were given fooWithBar and fooWithoutBar (see below at end) without knowing anything about them I'd like to be able to inspect them and figrue out know why they were not equal. Without getting back their list of referenced assemblies I cannot figure out why they are not equal.

Thanks,
Chris

        [TestMethod]
public void CompilationIdentity2() {

var foo = @"C:\Users\king\Documents\foo.dll";
var bar = @"C:\Users\king\Documents\bar.dll";

using (var file = new FileStream(bar, FileMode.Create))
Compilation.Create("bar.dll",
references: new[] {
},
syntaxTrees: new[] { SyntaxTree.ParseCompilationUnit("public class Bar { }") },
).Emit(file);

using (var file = new FileStream(foo, FileMode.Create))
Compilation.Create("foo.dll",
references: new[] {
},
syntaxTrees: new[] { SyntaxTree.ParseCompilationUnit("public class Foo : Bar { }") },
).Emit(file);

var withoutBar = Compilation.Create("wo", references: new[] { fooRef });
var withoutBar2 = Compilation.Create("wo", references: new[] { fooRef });
var withBar = Compilation.Create("wo", references: new[] { fooRef, barRef });

}