title: Fast creation of objects instead of Activator.CreateInstance(type)
body: <p>I'm trying to improve the performance of our application. We have a lot of Activator.CreateInstance calls that are causing some grief.</p>
  
  <p>We instantiate a lot of classes based on an interface (ITabDocument) and after looking around I thought of using this code:</p>
  
  <p>The code is no better (infact marginally slower) than using the Activator.CreateInstance code we had.</p>
  
  <pre><code>    public static Func&lt;T&gt; CreateInstance&lt;T&gt;(Type objType) where T : class, new()
      {
          var dynMethod = new DynamicMethod("DM$OBJ_FACTORY_" + objType.Name, objType, null, objType);
          ILGenerator ilGen = dynMethod.GetILGenerator();
          ilGen.Emit(OpCodes.Newobj, objType.GetConstructor(Type.EmptyTypes));
          ilGen.Emit(OpCodes.Ret);
          return (Func&lt;T&gt;)dynMethod.CreateDelegate(typeof(Func&lt;T&gt;));
      }
  </code></pre>
  
  <p>I'm wondering why this is, all I'm doing is:</p>
  
  <pre><code>ITabDocument document = CreateInstance&lt;ITabDocument&gt;(Type.GetType("[Company].Something"));
  </code></pre>
  
  <p>Is there a better way of creating objects which would assist with the above? Its a little hard when you're not sure of the concrete type.</p>