title: Recursive call to compare object properties
body: <pre><code>public static bool PropertiesEqual&lt;T&gt;(this T self, T other, string[] skip)
  {
      if (self.Equals(other)) return true;
  
          var primitive = (from p in typeof(T).GetProperties() 
                           where !skip.Contains(p.Name) 
                           &amp;&amp; p.PropertyType.IsSimpleType() 
                           select p).ToList();
  
          var rest = (from p in typeof(T).GetProperties()
                        where !p.PropertyType.IsSimpleType() select p).ToList();
  
          foreach(var pi in rest)
          {
              var selfValue = pi.GetValue(self, null);
              var otherValue = pi.GetValue(other, null);
  
              //var result = selfValue.PropertiesEqual(otherValue);
  
              if (!object.Equals(selfValue, otherValue))
                  return false;
          }
  
  
          foreach (var pi in primitive)
          {
              var selfValue = pi.GetValue(self, null);
              var otherValue = pi.GetValue(other, null);
  
              return object.Equals(selfValue, otherValue);
          }
  
          return true;
  }
  
  public static bool IsSimpleType(this Type type)
  {
      return (type.IsValueType || type.IsPrimitive || 
         type == typeof(String) || Convert.GetTypeCode(type) != TypeCode.Object);
  }
  </code></pre>
  
  <p>I'm using this method to compare equality on my entity instances. On the first level it works great, but I would like to iterate over <code>rest</code> (attached entities) and do a recursive call to this method (the comment line). </p>
  
  <p>The problem seems to be that <code>self</code> and <code>other</code> gets typed in to <code>object</code> on the recursive call, hence <code>primitive</code> gets zero results. If I inspect the type on <code>self</code> and <code>this</code> on the first level, I get the actual type, but on the second level I get <code>object</code>. I've tried using the <code>Convert.ChangeType</code> but it didn't help.</p>