title: Performance of dynamically created type
body: <p>Using C#, I am noticing a significant difference in perfomance when populating a list with instances of a dynamically generated type versus a simple struct. The code below includes 4 different methods for populating a list with 100,000 objects.</p>
  
  <p>Each method performs differently:</p>
  
  <p>Button1: 15 milliseconds</p>
  
  <p>Button2: 31 milliseconds</p>
  
  <p>Button3 &amp; 4: 300 milliseconds</p>
  
  <p>Note, the code for button 3 &amp; 4 came from <a href="https://stackoverflow.com/questions/1924733/list-of-dynamicly-created-structure">this topic</a></p>
  
  <p>Could anyone explain why the dynamically created object is slower?</p>
  
  <pre><code>    public struct DataRow
      {
          public double t;
          public double vf;
          public double im;
  
          public double T { get { return t; } set { t = value; } }
          public double Vf { get { return vf; } set { vf = value; } }
          public double Im { get { return im; } set { im = value; } }
      }
  
      //Use struct defined above
      private void button1_Click(object sender, EventArgs e)
      {
          int n = 0;
  
          //adding rows
          List&lt;DataRow&gt; myTable = new List&lt;DataRow&gt;();
          DataRow myRow = new DataRow();
  
          start = DateTime.Now;
  
          while (n &lt; 100000)
          {
              myRow.T = n * 1.0;
              myRow.Vf = 2.0;
              myRow.Im = 4.0;
  
              myTable.Add(myRow);
  
              n++;
          }
          end = DateTime.Now;
          System.TimeSpan diff = end.Subtract(start);
          label2.Text = diff.Seconds.ToString();
          label4.Text = diff.Milliseconds.ToString();
  
          dataGridView1.DataSource = myTable;
      }
  
      //define the list as it is done on buttons 3 &amp; 4 but use the static struct
      private void button2_Click(object sender, EventArgs e)
      {
          Type myType = typeof(DataRow);
  
          Type listType = typeof(List&lt;&gt;);
  
          Type myListType = listType.MakeGenericType(myType);
  
          IList myTable = (IList)Activator.CreateInstance(myListType);
  
          DataRow bRow = new DataRow();
  
          int n = 0;
          start = DateTime.Now;
          while (n &lt; 100000)
          {
              bRow.t = n * 1.0;
              bRow.vf = 2.0;
              bRow.im = 4.0;
              myTable.Add(bRow);
  
              n++;
          }
          end = DateTime.Now;
          System.TimeSpan diff = end.Subtract(start);
          label2.Text = diff.Seconds.ToString();
          label4.Text = diff.Milliseconds.ToString();
          dataGridView1.DataSource = myTable;
  
      }
  
      //Create assy at runtime and load dll
      private void button3_Click(object sender, EventArgs e)
      {
          Type myType = CreateDynRow();
          Assembly myAssy = Assembly.LoadFrom("DynaRowAssy.dll");
          Type myRow = myAssy.GetType("DynaRow");
  
          Type listType = typeof(List&lt;&gt;);
  
          Type myListType = listType.MakeGenericType(myRow);
  
          IList myTable = (IList)Activator.CreateInstance(myListType);
  
          FieldInfo piT = myRow.GetField("t");
          FieldInfo piVf = myRow.GetField("vf");
          FieldInfo piIm = myRow.GetField("im");
  
          ValueType aRow = (ValueType)Activator.CreateInstance(myRow);
  
          int n = 0;
          start = DateTime.Now;
          while (n &lt; 100000)
          {
              piT.SetValue(aRow, 1 * n);
              piVf.SetValue(aRow, 2.0);
              piIm.SetValue(aRow, 4.0);
              myTable.Add(aRow);
  
              n++;
          }
          end = DateTime.Now;
          System.TimeSpan diff = end.Subtract(start);
          label2.Text = diff.Seconds.ToString();
          label4.Text = diff.Milliseconds.ToString();
          dataGridView1.DataSource = myTable;
      }
  
      //create assy at runtime in memory
      private void button4_Click(object sender, EventArgs e)
      {
          //build the assembly
          Type myType = CreateDynRow();
  
          Type listType = typeof(List&lt;&gt;);
  
          Type myListType = listType.MakeGenericType(myType);
  
          IList myTable = (IList)Activator.CreateInstance(myListType);
  
          FieldInfo piT = myType.GetField("t");
          FieldInfo piVf = myType.GetField("vf");
          FieldInfo piIm = myType.GetField("im");
  
          ValueType aRow = (ValueType)Activator.CreateInstance(myType);
  
          int n = 0;
          start = DateTime.Now;
          while (n &lt; 100000)
          {
              piT.SetValue(aRow, 1 * n);
              piVf.SetValue(aRow, 2.0);
              piIm.SetValue(aRow, 4.0);
              myTable.Add(aRow);
  
              n++;
          }
          end = DateTime.Now;
          System.TimeSpan diff = end.Subtract(start);
          label2.Text = diff.Seconds.ToString();
          label4.Text = diff.Milliseconds.ToString();
          dataGridView1.DataSource = myTable;
      }
  </code></pre>