How to determine what other dll/exe is being used by a particular .net dll/exe? In simple words i want to find out the dependencies of a particular .net dll/exe which is in Global Assembly Cache. How can it be read via system.reflection?
-
3Assembly.GetReferencedAssemblies(). These are known dependencies, reflection code in the assembly can add unknown ones. You'd have to dig though embedded resources as well. COM interop and pinvoke adds more yet.Hans Passant– Hans Passant2014-08-06 11:47:29 +00:00Commented Aug 6, 2014 at 11:47
Add a comment
|
1 Answer
As @Hans Passant pointed out in his comment, you can use the GetReferencedAssemblies method to retrieve the referenced assemblies of a dll (i.e. the "known" dependencies):
var assembly = Assembly.Load("Microsoft.ActiveDirectory.Management, Version=6.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
var referencedAssemblies = assembly.GetReferencedAssemblies();
foreach (var referencedAssembly in referencedAssemblies)
{
Console.WriteLine(referencedAssembly.FullName);
}
The Assembly.Load() method will load an Assembly from the GAC if you fully qualify it with the Version, Culture, and PublicKeyToken. [reference]
2 Comments
Kashif Khan
Also can we load simply gac dll without providing the version?
skeryl
No, you have to supply the version of the assembly. Otherwise you will receive a
FileNotFoundException. You may be able to discover those additional details, however, by attempting to browse through the GAC's contents (such as in this answer: stackoverflow.com/questions/1599575/…)