title: Calling a dynamically loaded DLL method through reflection, but the target references a DLL
body: <p>I need to dynamically call a method in a bunch of DLLs that are also mine. There is an unknown amount of DLLs to call and they might be in different versions. I dynamically load each DLL and invoke a static method within them using reflection. Basically my code looks like this</p>
  
  <pre><code>foreach (var myThing in instances)
  {
      try {
          Assembly a = Assembly.LoadFile(myThing.DllPath);
          Type t = a.GetType("MyNamespace.Services");
          MethodInfo m = t.GetMethod("Backup");
          bool b = (bool)m.Invoke(null, new object[] { "Autobackup"+date+".zip", "Bot" });
      } catch (Exception e){
          LogInternalExceptions(e); // recursively vomit e.InternalException into logs
      }
  }
  </code></pre>
  
  <p>The problem is that the <code>Backup</code> method lives in a static class that references another DLL (log4net).</p>
  
  <pre><code>public static class Services
  {
      private static ILog Log = LogManager.GetLogger(typeof(Services));
  
      public static bool Backup(string filename, string comment)
      {
          // ... snip ...
          Log.Info("Aww right, Princess!");
          return true;
      }
  }
  </code></pre>
  
  <p>So, I get the expected error <code>Could not load file or assembly 'log4net, ...' or one of its dependencies. File not found.</code>.</p>
  
  <p>I'm sure that this is not my only issue with this method, but it's the first one and I'm stumped. I can edit the caller or the target code, but I don't really know how. The target is an Asp.net MVC3 application, but the class is just a pretty generic general purpose service class.</p>
  
  <p>I would wnat to use something like a preprocessor statement in the target; something like</p>
  
  <pre><code>#if REFLECTION
  Log.Info("Aww right, Princess!");
  #endif
  </code></pre>
  
  <p>But I know that makes no sense, because it's not in that stage, how else could I avoid this issue?</p>
  
  <p>Adding log4net reference to the caller project doesn't really solve the big picture. Meaning that if I have some references other than log4net, they would still causes an issue.</p>