2

there is a lot of examples how to load all dependencies from some assembly like:

var assembly = Assembly.ReflectionOnlyLoadFrom(assemblyPath);
foreach (var assemblyName in assembly.GetReferencedAssemblies()) {
  try {
    Assembly.ReflectionOnlyLoad(assemblyName.FullName);
  } catch {
    Assembly.ReflectionOnlyLoadFrom(Path.Combine(Path.GetDirectoryName(assemblyPath), assemblyName.Name + ".dll"));
  }
}

but what if one of dependencies is not ".dll" but ".exe" ? Do I need for that ".exe" assembly again to call recursivly GetReferencedAssemblies() in foreach loop ? Is there a danger of getting circular dependancy ?

br, Milan

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
milan
  • 213
  • 3
  • 10

4 Answers4

4

You should be able to use the exe just like any other dll. It just has this extra bonus that it can be executed standalone.

Simon D.
  • 4,451
  • 2
  • 28
  • 57
  • First question is how to recognize that some assemblyName is exe ? I get just its name e.g. "MyFile" but no extension. In my case I know that "MyFile" is exe but in general case I do not know its extension, so the last line in code example I gave will give exception. How to deal witht that, any idea ? another exception handler would be too much... – milan Oct 20 '10 at 09:06
  • Assembly.CodeBase should tell you the file name. System.IO.File... should enable you to get the extension, but I think you don't need the file-extension anymore, once you have the filename. – Simon D. Oct 21 '10 at 11:42
2

An assembly that is a dll can just as well as an exe have references to other assemblies; there is no difference between them from that perspective.

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
0

As mentioned, you may treat an .exe like a .dll in terms of referencing.

As for worrying about circular references, would those libraries compile if there was one? Just a thought.

Here's some quick recursive code to get all dependent assemblies:

    private static IEnumerable<Assembly> GetAllDependencies(Assembly assembly)
    {
        var dict = new Dictionary<string, AssemblyName>();
        dict.Add(assembly.GetName().FullName, assembly.GetName());
        dict = GetAllDependenciesRecursive(assembly.GetName(), dict);
        return dict.Select(d => Assembly.Load(d.Value)).ToArray();
    }

    private static Dictionary<string, AssemblyName> GetAllDependenciesRecursive(AssemblyName assemblyName, Dictionary<string, AssemblyName> existingRefList)
    {
        var assembly = Assembly.Load(assemblyName);
        List<AssemblyName> a = assembly.GetReferencedAssemblies().ToList();
        foreach (var refAssemblyName in a)
        {
            if (!existingRefList.ContainsKey(refAssemblyName.FullName))
            {
                existingRefList.Add(refAssemblyName.FullName, refAssemblyName);
                existingRefList = GetAllDependenciesRecursive(refAssemblyName, existingRefList);
            }
        }
        return existingRefList;
    }
  • Circular references **do** happen - see [this answer](http://stackoverflow.com/a/1305043/1364007) by [Marc Gravell](http://stackoverflow.com/users/23354/marc-gravell), an [MVP](https://mvp.microsoft.com/). – Wai Ha Lee Jan 14 '16 at 13:32
-1

GetReferencedAssemblies() does not return the entire dependency tree (dll or exe doesn't make a difference), so you will need a solution that traverses the tree. See this article http://msdn.microsoft.com/en-us/magazine/cc163641.aspx.

insipid
  • 3,238
  • 3
  • 26
  • 38
  • If GetReferencedAssemblies does not return all assemblies is there any option using reflection methods to retrieve all dependencies ? – milan Oct 20 '10 at 12:56
  • The article explains how to get all the dependencies using the GetReferencedAssemblies() method. As for a single method that returns the whole tree, I do not know of one myself. – insipid Oct 21 '10 at 13:51
  • Dead link. To bad you didn't actually put the content in the answer :( – Erik Philips Nov 03 '16 at 21:05