title: C# Late Binding Unloading Dll in Runtime
body: <p><strong>I want to load <code>.dll</code> into my own c# program in run-time as a plugin</strong>.If there's new update unload existing plugin dll (<em>InvokeHelper.dll</em>) and download the updated<code>.dll</code> (<em>InvokeHelper.dll</em>) and load it into the program without terminating the process.(my application). I read these articles to complete these task(<a href="https://msdn.microsoft.com/en-us/library/system.appdomain.aspx" rel="nofollow noreferrer">MSDN,</a> <a href="https://stackoverflow.com/q/4887847/1641556">Article2</a>) But still this code doesn't work for unloading <code>.dll</code> dynamically.</p>
  
  <p>When I looked attached dll via process explorer attached dll is not unloaded.It's exist in <a href="https://technet.microsoft.com/en-us/sysinternals/processexplorer.aspx" rel="nofollow noreferrer">process explorer</a> dll list.I think if the dll unload success I want to manually rename the old dll and replace the new one.</p>
  
  <p><strong>InvokeHelper.dll</strong> Source:</p>
  
  <pre><code>using System;
  
  namespace InvokeHelper
  {
      public class LateBindingInvokeHelper
      {
          public void PrintHello()
          {
              using (System.IO.StreamWriter w = new System.IO.StreamWriter(@"Invoker.txt", true))
              {
                  w.WriteLine(DateTime.Now.ToString());
                  w.Flush();
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Load Dll Function:</strong></p>
  
  <pre><code> private AppDomain domain = null;
   private void LoadDll()
          {
              AppDomainSetup domInfo = new AppDomainSetup();
              domInfo.ApplicationBase = System.Environment.CurrentDirectory;
              Evidence adevidence = AppDomain.CurrentDomain.Evidence;
              domInfo.DisallowBindingRedirects = false;
              domInfo.DisallowCodeDownload = true;
              domain = AppDomain.CreateDomain("MyDomain", adevidence , domInfo);
  
              Type type = typeof(Proxy);
              var value = (Proxy)domain.CreateInstanceAndUnwrap(
                  type.Assembly.FullName,
                  type.FullName);
  
              var assembly = value.GetAssembly(@"C:\dev\ExBinder\ExBinder\bin\Debug\InvokeHelper.dll");
              Type[] mytypes = assembly.GetTypes();
              BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
              foreach (Type t in mytypes)
              {
                  MethodInfo[] mi = t.GetMethods(flags); // you can change this flag , commented some flags :) view only plublic or bla bla 
                  Object obj = Activator.CreateInstance(t);
                  foreach (MethodInfo m in mi)
                  {
                      if (m.Name.Equals("PrintHello")) m.Invoke(obj, null); //my func name :) you can set this via config :D
                  }
              }
          }
  
      private void button2_Click(object sender, EventArgs e)
      {
          LoadDll();
          AppDomain.Unload(domain);
      }
  </code></pre>
  
  <p><strong>Proxy Class</strong></p>
  
  <pre><code>public class Proxy : MarshalByRefObject
      {
          public Assembly GetAssembly(string assemblyPath)
          {
              try
              {
                  return Assembly.LoadFile(assemblyPath);
              }
              catch (Exception)
              {
                  return null;               
              }
          }
      }
  </code></pre>
  
  <ul>
  <li>Source to <a href="https://drive.google.com/file/d/0BxnNqc-EDIMiX1hqS0VMVHgzOVk/view?usp=sharing" rel="nofollow noreferrer">download</a>  </li>
  <li>Image full size <a href="https://drive.google.com/file/d/0BxnNqc-EDIMiTU5KTUVhcWJBMmM/view?usp=sharing" rel="nofollow noreferrer">here</a></li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/gscl4.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gscl4.jpg" alt="enter image description here"></a></p>