title: Comparing two objects' properties simply in c#
body: <p>I have a class with lots of properties. A shallow copy is enough to fully replicate the object.</p>
  
  <p>I need to compare an object just to check if it contains exactly the same values as another.</p>
  
  <p>My ideas:</p>
  
  <p>The first and most obvious solution is just to create a huge method block that compares each property, one after the other.</p>
  
  <p>The second would be to serialize each object and hash the file or do some sort of md5 checksum on it. (Would this actually work?)</p>
  
  <p>The third is to do some sort of reflection on the object, which would automate the first option, but create an added level of complexity.</p>
  
  <p>Speed isn't really an issue.</p>
  
  <p>I'm interested to hear thoughts, or any other methods I am missing to do such a thing.</p>
  
  <p>Edit:
  Thanks all. My solution (Modified to now be recursive on generic items):</p>
  
  <pre><code>public static bool IsSame&lt;T&gt;(T objA, T objB)
  {
      var type = typeof(T);
      var properties = type.GetProperties();
      foreach (var pi in properties.Where(a =&gt; a.CanRead))
      {
          if (pi.Name == "Item")
          {
              var piCount = properties.FirstOrDefault(a =&gt; a.Name == "Count");
              int count = -1;
              if (piCount != null &amp;&amp; piCount.PropertyType == typeof(System.Int32))
                  count = (int)piCount.GetValue(objA, null);
              if (count &gt; 0)
              {
                  for (int i = 0; i &lt; count; i++)
                  {
                      dynamic child1 = pi.GetValue(objA, new object[] { i });
                      dynamic child2 = pi.GetValue(objB, new object[] { i });
                      return IsSame(child1, child2);
                  }
              }
          }
          else
          {
              var val1 = type.GetProperty(pi.Name).GetValue(objA, null);
              var val2 = type.GetProperty(pi.Name).GetValue(objB, null);
              if (val1 != val2 &amp;&amp; (val1 == null || !val1.Equals(val2)))
                  return false;
          }
      }
      return true;
  }
  </code></pre>