title: Revit Api Load Command - Auto Reload
body: <p>I'm working with the revit api, and one of its problems is that it locks the .dll once the command's run. You have to exit revit before the command can be rebuilt, very time consuming.</p>
  
  <p>After some research, I came across this post on GitHub, that streams the command .dll into memory, thus hiding it from Revit. Letting you rebuild the VS project as much as you like.</p>
  
  <p>The AutoReload Class impliments the revit IExteneralCommand Class which is the link into the Revit Program. </p>
  
  <p>But the AutoReload class hides the actual source DLL from revit. So revit can't lock the DLL and lets one rebuilt the source file.</p>
  
  <p>Only problem is I cant figure out how to implement it, and have revit execute the command. I guess my C# general knowledge is still too limited.</p>
  
  <p>I created an entry in the RevitAddin.addin manifest that points to the AutoReload Method command, but nothing happens. </p>
  
  <p>I've tried to follow all the comments in the posted code, but nothing seems to work; and no luck finding a contact for the developer.</p>
  
  <p>Found at: <a href="https://gist.github.com/6084730.git" rel="nofollow">https://gist.github.com/6084730.git</a></p>
  
  <pre><code>using System;
  
  namespace Mine
  {
      //  helper class
      public class PluginData
      {
          public DateTime _creation_time;
          public Autodesk.Revit.UI.IExternalCommand _instance;
  
      public PluginData(Autodesk.Revit.UI.IExternalCommand instance)
      {
          _instance = instance;
      }
  }
  
  //
  //   Base class for auto-reloading external commands that reside in other dll's
  //   (that Revit never knows about, and therefore cannot lock)
  //
  public class AutoReload : Autodesk.Revit.UI.IExternalCommand
  {
      // keep a static dictionary of loaded modules (so the data persists between calls to Execute)
      static System.Collections.Generic.Dictionary&lt;string, PluginData&gt; _dictionary;
  
      String _path;   // to the dll
      String _class_full_name;
  
      public AutoReload(String path, String class_full_name)
      {
          if (_dictionary == null)
          {
              _dictionary = new System.Collections.Generic.Dictionary&lt;string, PluginData&gt;();
          }
          if (!_dictionary.ContainsKey(class_full_name))
          {
              PluginData data = new PluginData(null);
              _dictionary.Add(class_full_name, data);
          }
          _path = path;
          _class_full_name = class_full_name;
      }
  
      public Autodesk.Revit.UI.Result Execute(
          Autodesk.Revit.UI.ExternalCommandData commandData, 
          ref string message, 
          Autodesk.Revit.DB.ElementSet elements)
      {
          PluginData data = _dictionary[_class_full_name];
          DateTime creation_time = new System.IO.FileInfo(_path).LastWriteTime;
          if (creation_time.CompareTo(data._creation_time) &gt; 0)
          {
              //  dll file has been modified, or this is the first time we execute this command.
              data._creation_time = creation_time;
              byte[] assembly_bytes = System.IO.File.ReadAllBytes(_path);
              System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(assembly_bytes);
              foreach (Type type in assembly.GetTypes())
              {
                  if (type.IsClass &amp;&amp; type.FullName == _class_full_name)
                  {
                      data._instance = Activator.CreateInstance(type) as Autodesk.Revit.UI.IExternalCommand;
                      break;
                  }
              }
          }
          // now actually call the command
          return data._instance.Execute(commandData, ref message, elements);
      }
  }
  
  //
  //   Derive a class from AutoReload for every auto-reloadable command. Hardcode the path 
  //   to the dll and the full name of the IExternalCommand class in the constructor of the base class.
  //
  [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
  [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
  public class AutoReloadExample : AutoReload
  {
      public AutoReloadExample()
          : base("C:\\revit2014plugins\\ExampleCommand.dll", "Mine.ExampleCommand")
      {
      }
      }
  
  }
  </code></pre>