Reputation: 2579
I am trying to get the dependency assemblies name, the location from where the dependent DLL is loading and the dependency of the dependent DLL's.
I am getting the path of the ".exe" file as an input.
To find the dependencies I am using the following code.
var assemblies = Assembly.LoadFile("C:\\My Folder\\ MyApp.exe").GetReferencedAssemblies();
The "assemblies" is a collection of System.Reflection.AssemblyName
class objects.
I am using the for-each to get the list of assembly names to find the dependency list.
How to get the location of each dependency assembly?
How to get the dependency of dependent assemblies? For example, if am using myAppBase.dll in the MyApp application, How can I get the dependencies of myAppBase.dll.
Upvotes: 3
Views: 2002
Reputation: 38870
Something like this should get all referenced assemblies. To convert the AssemblyName to an Assembly you have to load it. This will return an enumerable with all referenced assemblies. You don't need to pass the HashSet in, it's just for recursive calls to prevent infinite loops.
If you don't want to keep these loaded, I recommend loading them into a separate AppDomain and unloading it afterwards.
private static IEnumerable<Assembly> GetReferencedAssemblies(Assembly a, HashSet<string> visitedAssemblies = null)
{
visitedAssemblies = visitedAssemblies ?? new HashSet<string>();
if (!visitedAssemblies.Add(a.GetName().EscapedCodeBase))
{
yield break;
}
foreach (var assemblyRef in a.GetReferencedAssemblies())
{
if (visitedAssemblies.Contains(assemblyRef.EscapedCodeBase)) { continue; }
var loadedAssembly = Assembly.Load(assemblyRef);
yield return loadedAssembly;
foreach (var referenced in GetReferencedAssemblies(loadedAssembly, visitedAssemblies))
{
yield return referenced;
}
}
}
As for DLL location, you can use Location
on the Assembly
object to retrieve that.
Upvotes: 3