3

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.

  1. How to get the location of each dependency assembly?

  2. 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.

3
  • 3
    @Stefan MSDN answers 1) by looking at "Location" on "Assembly". 2) If he can get the depdencencies of his application from the assembly object, then surely he can get the referenced assemblies' referenced assemblies from their assembly objects? I just see the question as a lack of reading the docs and a lack of logical thought on the process. In general, it seems to be low effort. Commented Dec 28, 2017 at 9:02
  • @john: Thanks for your comments. Could you please let me how to get the assembly object from the AssemblyName class? Commented Dec 28, 2017 at 9:12
  • 1
    I've added my suggestion. Commented Dec 28, 2017 at 9:14

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.