title: Writing C# Plugin System
body: <p>I'm trying to write a plugin system to provide some extensibility to an application of mine so someone can write a plugin(s) for the application without touching the main application's code (and risk breaking something).</p>
  
  <p>I've got the base "IPlugin" interface written (atm, nothing is implemented yet)</p>
  
  <p>Here is how I'm loading:</p>
  
  <pre><code>public static void Load()
  {
      // rawr: http://www.codeproject.com/KB/cs/c__plugin_architecture.aspx
      String[] pluginFiles = Directory.GetFiles(Plugins.PluginsDirectory, "*.dll");
      foreach (var plugin in pluginFiles)
      {
          Type objType = null;
          try
          {
              //Assembly.GetExecutingAssembly().GetName().Name
              MessageBox.Show(Directory.GetCurrentDirectory());
              Assembly asm = Assembly.Load(plugin);
              if (asm != null)
              {
                  objType = asm.GetType(asm.FullName);
                  if (objType != null)
                  {
                      if (typeof(IPlugin).IsAssignableFrom(objType))
                      {
                          MessageBox.Show(Directory.GetCurrentDirectory());
                          IPlugin ipi = (IPlugin)Activator.CreateInstance(objType);
                          ipi.Host = Plugins.m_PluginsHost;
                          ipi.Assembly = asm;
                      }
                  }
              }
          }
          catch (Exception e)
          {
              MessageBox.Show(e.ToString(), "Unhandled Exception! (Please Report!)", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
          }
      }
  }
  </code></pre>
  
  <p>A friend tried to help but I really didn't understand what was wrong.</p>
  
  <p>The folder structure for plugins is the following:</p>
  
  <p>\ <br/>
  \Plugins\ <br /></p>
  
  <p>All plugins reference a .dll called "Lab.Core.dll" in the [root] directory and it is not present in the Plugins directory because of duplicate references being loaded.</p>
  
  <p>The plugin system is loaded from Lab.Core.dll which is also referenced by my executable. Type "IPlugin" is in Lab.Core.dll as well. Lab.Core.dll is, exactly as named, the core of my application.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Question: Why/What is that exception I'm getting and how could I go about fixing it?</p>
  
  <p><strong>FINAL EDIT:</strong></p>
  
  <p>Ok so I decided to re-write it after looking at some source code a friend wrote for a TF2 regulator.</p>
  
  <p>Here's what I got and it works:</p>
  
  <pre><code>    public class TestPlugin : IPlugin {
      #region Constructor
  
      public TestPlugin() {
          //
      }
  
      #endregion
  
      #region IPlugin Members
  
      public String Name {
          get {
              return "Test Plugin";
          }
      }
  
      public String Version {
          get {
              return "1.0.0";
          }
      }
  
      public String Author {
          get {
              return "Zack";
          }
      }
  
      public Boolean OnLoad() {
          MessageBox.Show("Loaded!");
          return true;
      }
  
      public Boolean OnAllLoaded() {
          MessageBox.Show("All loaded!");
          return true;
      }
  
      #endregion
  }
  
          public static void Load(String file) {
          if (!File.Exists(file) || !file.EndsWith(".dll", true, null))
              return;
  
          Assembly asm = null;
  
          try {
              asm = Assembly.LoadFile(file);
          } catch (Exception) {
              // unable to load
              return;
          }
  
          Type pluginInfo = null;
          try {
              Type[] types = asm.GetTypes();
              Assembly core = AppDomain.CurrentDomain.GetAssemblies().Single(x =&gt; x.GetName().Name.Equals("Lab.Core"));
              Type type = core.GetType("Lab.Core.IPlugin");
              foreach (var t in types)
                  if (type.IsAssignableFrom((Type)t)) {
                      pluginInfo = t;
                      break;
                  }
  
              if (pluginInfo != null) {
                  Object o = Activator.CreateInstance(pluginInfo);
                  IPlugin plugin = (IPlugin)o;
                  Plugins.Register(plugin);
              }
          } catch (Exception) {
          }
      }
  
      public static void LoadAll() {
          String[] files = Directory.GetFiles("./Plugins/", "*.dll");
          foreach (var s in files)
              Load(Path.Combine(Environment.CurrentDirectory, s));
  
          for (Int32 i = 0; i &lt; Plugins.List.Count; ++i) {
              IPlugin p = Plugins.List.ElementAt(i);
              try {
                  if (!p.OnAllLoaded()) {
                      Plugins.List.RemoveAt(i);
                      --i;
                  }
              } catch (Exception) {
                  Plugins.List.RemoveAt(i);
                  --i;
              }
          }
      }
  </code></pre>