title: Copy object properties: reflection or serialization - which is faster?
body: <p>I have two objects of the same type and need to copy property values from one object to another. There are two options:</p>
  
  <ol>
  <li><p>Use reflection, navigate through the properties of the first object and copy the values.</p></li>
  <li><p>Serialize the first object and deserialize a copy.</p></li>
  </ol>
  
  <p>Both work for my requirement, the question is which do I better use in the terms of speed (cost)?</p>
  
  <p><strong>Example</strong></p>
  
  <pre><code>class Person
  {
      public int ID { get; set; }
      public string Firsthand { get; set; } 
      public string LastName { get; set; } 
      public int Age { get; set; } 
      public decimal Weight { get; set; } 
  }
  </code></pre>
  
  <p>Need to copy property values from <code>Person p1</code> to <code>Person p2</code>.</p>
  
  <p>For this simple sample - which method is faster?</p>
  
  <p><strong>Update</strong></p>
  
  <p>For serialization I use the ObjectCopier suggested here: <a href="https://stackoverflow.com/questions/78536/cloning-objects-in-c-sharp">Deep cloning objects</a></p>
  
  <p>For reflection I use this code:</p>
  
  <pre><code>foreach (PropertyInfo sourcePropertyInfo in copyFromObject.GetType().GetProperties())  
  {
      PropertyInfo destPropertyInfo = copyToObject.GetType().GetProperty(sourcePropertyInfo.Name);
  
      destPropertyInfo.SetValue(
          copyToObject,
          sourcePropertyInfo.GetValue(copyFromObject, null),
          null);
  }
  </code></pre>