title: How to compile C# DLL on the fly, Load, and Use
body: <p>A) compiling C# EXE's and DLL's on the fly are relatively easy.<br>
  B) Executing an EXE means that a new application is run. Loading a DLL means that methods and functions can be used in cases that may be shared between applications or projects.<br>
  <br>
  Now, the quickest and easiest way to compile your EXE (or with mild modifications, DLL) can be found from the <a href="http://msdn.microsoft.com/en-us/library/saf5ce06%28v=vs.110%29.aspx" rel="nofollow">MSDN</a> or for your convenience:<br></p>
  
  <pre><code>private bool CompileCSharpCode(string script)
  {
  lvErrors.Items.Clear();
      try
      {
          CSharpCodeProvider provider = new CSharpCodeProvider();
          // Build the parameters for source compilation.
          CompilerParameters cp = new CompilerParameters
          {
              GenerateInMemory = false,
              GenerateExecutable = false, // True = EXE, False = DLL
              IncludeDebugInformation = true,
              OutputAssembly = "eventHandler.dll", // Compilation name
          };
  
          // Add in our included libs.
          cp.ReferencedAssemblies.Add("System.dll");
          cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
          cp.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll");
  
          // Invoke compilation. This works from a string, but you can also load from a file using FromFile()
          CompilerResults cr = provider.CompileAssemblyFromSource(cp, script);
          if (cr.Errors.Count &gt; 0)
          {
              // Display compilation errors.
              foreach (CompilerError ce in cr.Errors)
              {
                  //I have a listview to display errors.
                  lvErrors.Items.Add(ce.ToString());
              }
              return false;
          }
          else
          {
              lvErrors.Items.Add("Compiled Successfully.");
          }
          provider.Dispose();
      }
      catch (Exception e)
      {
          // never really reached, but better safe than sorry?
          lvErrors.Items.Add("SEVERE! "+e.Message + e.StackTrace.ToString());
          return false;
      }
      return true;
  }
  </code></pre>
  
  <p>Now that you can compile on the fly, there are a few variances between how to load the DLL. Typically speaking, you would add it as a reference in Visual Studios to be compiled into the project. This is rather easy and you have probably done it many times over, but we want to use it in our current project, and we can't very well require the user to recompile the entire project every time they want to test out their new DLL. Therefor, I will simply discuss how one loads a library 'on the fly'. Another term here would by "programmatically". To do this, after a successful compile, we load up an Assembly as follows:<br></p>
  
  <pre><code>Assembly assembly = Assembly.LoadFrom("yourfilenamehere.dll");
  </code></pre>
  
  <p>If you have an AppDomain, you can try this:<br></p>
  
  <pre><code>Assembly assembly = domain.Load(AssemblyName.GetAssemblyName("yourfilenamehere.dll"));
  </code></pre>
  
  <p><br>
  Now that the lib is "referenced", we can open it up and use it. There are 2 ways to do this. One requires you to know if the method has parameters, another will check for you. I'll do the later, you can check the <a href="http://msdn.microsoft.com/en-us/library/25y1ya39.aspx" rel="nofollow">MSDN</a> for the other.<br></p>
  
  <pre><code>// replace with your namespace.class
  Type type = assembly.GetType("company.project");
  if (type != null)
  {
      // replace with your function's name
      MethodInfo method = type.GetMethod("method");
  
      if (method != null)
      {
          object result = null;
          ParameterInfo[] parameters = method.GetParameters();
          object classInstance = Activator.CreateInstance(type, null);
          if (parameters.Length == 0) // takes no parameters
          {
                  // method A:
              result = method.Invoke(classInstance, null);
                  // method B:
              //result = type.InvokeMember("method", BindingFlags.InvokeMethod, null, classInstance, null);
          }
          else // takes 1+ parameters
          {
              object[] parametersArray = new object[] { }; // add parameters here
  
                  // method A:
              result = method.Invoke(classInstance, parametersArray);
                  // method B:
              //result = type.InvokeMember("method", BindingFlags.InvokeMethod, null, classInstance, parametersArray);
          }
      }
  }
  </code></pre>
  
  <p>PROBLEM:
  First compile works fine. First execution works fine. However, the recompile attempt will error, saying that your *.PDP (debugger database) is in use. I've heard some hints about marshaling, and AppDomains, but I haven't quite cleared up the problem. Recompile will only fail after the DLL has been loaded.</p>
  
  <hr>
  
  <p>Current attempt at Marshaling &amp;&amp; AppDomain:</p>
  
  <pre><code>class ProxyDomain : MarshalByRefObject
      {
          private object _instance;
          public object Instance
          {
              get { return _instance; }
          }
          private AppDomain _domain;
          public AppDomain Domain
          {
              get
              {
                  return _domain;
              }
          }
          public void CreateDomain(string friendlyName, System.Security.Policy.Evidence securityinfo)
          {
              _domain = AppDomain.CreateDomain(friendlyName, securityinfo);
          }
          public void UnloadDomain()
          {
              try
              {
                  AppDomain.Unload(_domain);
              }
              catch (ArgumentNullException dne)
              {
                  // ignore null exceptions
                  return;
              }
          }
          private Assembly _assembly;
          public Assembly Assembly
          {
              get
              {
                  return _assembly;
              }
          }
          private byte[] loadFile(string filename)
          {
              FileStream fs = new FileStream(filename, FileMode.Open);
              byte[] buffer = new byte[(int)fs.Length];
              fs.Read(buffer, 0, buffer.Length);
              fs.Close();
  
              return buffer;
          }
          public void LoadAssembly(string path, string typeName)
          {
              try
              {
                  if (_domain == null)
                      throw new ArgumentNullException("_domain does not exist.");
                  byte[] Assembly_data = loadFile(path);
                  byte[] Symbol_data = loadFile(path.Replace(".dll", ".pdb"));
  
                  _assembly = _domain.Load(Assembly_data, Symbol_data);
                  //_assembly = _domain.Load(AssemblyName.GetAssemblyName(path));
                  _type = _assembly.GetType(typeName);
              }
              catch (Exception ex)
              {
                  throw new InvalidOperationException(ex.ToString());
              }
          }
          private Type _type;
          public Type Type
          {
              get
              {
                  return _type;
              }
          }
          public void CreateInstanceAndUnwrap(string typeName)
          {
              _instance = _domain.CreateInstanceAndUnwrap(_assembly.FullName, typeName);
          }
      }
  </code></pre>
  
  <p>Errors on _instance = _domain.CreateInstanceAndUnwrap(_assembly.FullName, typeName); saying that my Assembly isn't serializable. Tried adding [Serializable] tag to my class with no luck. Still researching fixes.</p>
  
  <p>Seems things can get a bit confusing when you can't see how they're being used, so here's making it easy?</p>
  
  <pre><code>private void pictureBox1_Click(object sender, EventArgs e)
      {
          pd.UnloadDomain();
  
          if (CompileCSharpCode(header + tScript.Text + footer))
          {
              try
              {
                  pd.CreateDomain("DLLDomain", null);
                  pd.LoadAssembly("eventHandler.dll", "Events.eventHandler");
                  pd.CreateInstanceAndUnwrap("Events.eventHandler"); // Assembly not Serializable error!
  
                  /*if (pd.type != null)
                  {
                      MethodInfo onConnect = pd.type.GetMethod("onConnect");
  
                      if (onConnect != null)
                      {
                          object result = null;
                          ParameterInfo[] parameters = onConnect.GetParameters();
                          object classInstance = Activator.CreateInstance(pd.type, null);
                          if (parameters.Length == 0)
                          {
                              result = pd.type.InvokeMember("onConnect", BindingFlags.InvokeMethod, null, classInstance, null);
                              //result = onConnect.Invoke(classInstance, null);
                          }
                          else
                          {
                              object[] parametersArray = new object[] { };
  
                              //result = onConnect.Invoke(classInstance, parametersArray);
                              //result = type.InvokeMember("onConnect", BindingFlags.InvokeMethod, null, classInstance, parametersArray);
                          }
                      }
                  }*/
                  //assembly = Assembly.LoadFrom(null);
  
              }
              catch (Exception er)
              {
                  MessageBox.Show("There was an error executing the script.\n&gt;" + er.Message + "\n   - " + er.StackTrace.ToString());
              }
              finally
              {
              }
          }
      }
  </code></pre>