title: Dynamic loading of DLL
body: <p>I have a program which needs to use a large number of plugins.</p>
  
  <p>Each plugin must support a very basic interface, this interface is defined in a DLL (IBaseComponent for simplicity of question sake).</p>
  
  <p>Each plugin will be in a specific directory (AppDirectory\plugin\plugin-type).
  Each plugin can have any name for the plugin's dll (AppDirectory\plugin\plugin-type\plugin-name.dll).</p>
  
  <p>So I need to check through each plugin subdirectory, find each plugin that has a class which supports the IBaseComponent interface, instantiate the class and call some functions on the plug in.</p>
  
  <p>ok, all fine and dandy, none of this is particularly hard. The problem though is that I seem to be running into some weird issues.</p>
  
  <p>Every plugin needs to have the Base.dll file in the individual plugin folders (instead of just in the program that will be loading the plugin) and also it seems that I get many errors and warnings around dynamically loading a dll which have dll's that also need to be loaded.</p>
  
  <p>I'm using: </p>
  
  <pre><code>pluginModule = System.Reflection.Assembly.ReflectionOnlyLoadFrom(PathToAssembly);
  </code></pre>
  
  <p>in order to grab the plugin dll and using:</p>
  
  <pre><code>types = moduleAssembly.GetTypes();
  </code></pre>
  
  <p>in order to grab the types contained within the dll. 
  I am iterating over the types and checking if the individual type is of the IBaseComponent interface (signifying this is a valid to load class) with:</p>
  
  <pre><code>if (type.GetInterface("FrameworkNameSpace.IBaseComponent") != null)
      //it's of the IBaseComponent interface
  </code></pre>
  
  <p>Later, in order to actually create an instance of the class from the dll I use:</p>
  
  <pre><code>pluginModule = System.Reflection.Assembly.LoadFrom(PathToAssembly);
  </code></pre>
  
  <p>and then use:</p>
  
  <pre><code>types = component.GetTypes();
  </code></pre>
  
  <p>In order to get the types within the module then select and load the class which supports the interface same as above.</p>
  
  <p>The problem seems to be on when I use:</p>
  
  <pre><code>types = component.GetTypes();
  </code></pre>
  
  <p>When actually trying to load the class, Instead of simply looking at it. (Hence my different usage of <em>LoadFrom</em> and <em>ReflectionOnlyLoad</em>)</p>
  
  <p>The Exception I receive on the GetTypes call (on a second plug in, but never the first!) is:</p>
  
  <pre><code>{"Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."}
  </code></pre>
  
  <p>With the LoaderExceptions property as:</p>
  
  <pre><code>{"The specified module could not be found. (Exception from HRESULT: 0x8007007E)":null}
  </code></pre>
  
  <p>I'm unsure why this occurs. The DLL is in the plugin folder, and the DLL containing the IBaseComponent interface is also in every one of the plugin directories. Am I going about this the wrong way?</p>
  
  <p>Also is it required for me to keep a copy of the DLL containing IBaseComponent within each plugin subdirectory as well as the one used by the program itself, or am I doing something incorrectly that would allow me to remove this requirement?</p>
  
  <p>I am aware of MEF which is what I wanted to use but unfortunately because I am required to support this on .net 2.0  I am unable to use MEF.</p>