How can I get a list of assemblies referenced from a given assembly?
-
2011년 12월 4일 일요일 오전 3:08
Is there a way to get all the assemblies referenced from a given assembly or module?
Thanks,
Chris- 편집됨 kingces95 2011년 12월 4일 일요일 오전 3:08
모든 응답
-
2011년 12월 4일 일요일 오후 6: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 -
2011년 12월 4일 일요일 오후 7:44
Filip, hi, given an AssemblySymbol.
Thanks,
Chris- 답변으로 표시됨 Anthony D. GreenOwner 2011년 12월 15일 목요일 오후 12:23
-
2011년 12월 5일 월요일 오후 10:05소유자
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- 답변으로 표시됨 kingces95 2011년 12월 5일 월요일 오후 10:41
-
2011년 12월 5일 월요일 오후 10: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
-
2011년 12월 5일 월요일 오후 10:58소유자
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 -
2011년 12월 6일 화요일 오전 1:37
-
2011년 12월 6일 화요일 오후 7:11소유자Awesome - Thank you Chris!
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team -
2012년 1월 17일 화요일 오전 12: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 -
2012년 2월 14일 화요일 오후 9: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 -
2012년 2월 14일 화요일 오후 10: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[] { 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); }

