How can I get a list of assemblies referenced from a given assembly?
-
Sunday, December 04, 2011 3:08 AM
Is there a way to get all the assemblies referenced from a given assembly or module?
Thanks,
Chris- Edited by kingces95 Sunday, December 04, 2011 3:08 AM
All Replies
-
Sunday, December 04, 2011 6:09 PM
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 -
Sunday, December 04, 2011 7:44 PM
Filip, hi, given an AssemblySymbol.
Thanks,
Chris- Marked As Answer by Anthony D. GreenOwner Thursday, December 15, 2011 12:23 PM
-
Monday, December 05, 2011 10:05 PMOwner
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- Marked As Answer by kingces95 Monday, December 05, 2011 10:41 PM
-
Monday, December 05, 2011 10:26 PM
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
-
Monday, December 05, 2011 10:58 PMOwner
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)...
Regarding Connect, Karen had posted a response to your question at the bottom of this thread - http://social.msdn.microsoft.com/Forums/en-US/roslyn/thread/8b0089ae-2419-4af1-8ece-5ffa7d2379a6 :)
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team -
Tuesday, December 06, 2011 1:37 AM
-
Tuesday, December 06, 2011 7:11 PMOwnerAwesome - Thank you Chris!
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team -
Tuesday, January 17, 2012 12:03 AM
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 -
Tuesday, February 14, 2012 9:35 PM
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 -
Tuesday, February 14, 2012 10:02 PM
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[] { MetadataReference.Create(@"C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll"), }, syntaxTrees: new[] { SyntaxTree.ParseCompilationUnit("public class Bar { }") }, options: new CompilationOptions(assemblyKind: AssemblyKind.DynamicallyLinkedLibrary) ).Emit(file); using (var file = new FileStream(foo, FileMode.Create)) Compilation.Create("foo.dll", references: new[] { MetadataReference.Create(@"C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll"), MetadataReference.Create(bar) }, syntaxTrees: new[] { SyntaxTree.ParseCompilationUnit("public class Foo : Bar { }") }, options: new CompilationOptions(assemblyKind: AssemblyKind.DynamicallyLinkedLibrary) ).Emit(file); var fooRef = MetadataReference.Create(foo); var barRef = MetadataReference.Create(bar); 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 }); var fooWithoutBar = withoutBar.GetTypeByMetadataName("Foo"); var fooWithoutBar2 = withoutBar2.GetTypeByMetadataName("Foo"); var fooWithBar = withBar.GetTypeByMetadataName("Foo"); Assert.IsNotNull(fooWithoutBar); Assert.IsNotNull(fooWithoutBar2); Assert.IsNotNull(fooWithBar); Assert.AreSame(fooWithoutBar, fooWithoutBar2); Assert.AreNotSame(fooWithoutBar, fooWithBar); }

