title: Implementing IEqualityComparer<T> for comparing arbitrary properties of any class (including anonymous)
body: <p>I am writing this neat class implementing IEqualityComparer, so that I can just pass any anonymous type to it (or actually any type with properties) and it will automatically compare the types by comparing the property values of the types.</p>
  
  <pre><code>public class CompareProperty&lt;T&gt; : IEqualityComparer&lt;T&gt;
      {
          private Type type;
          private PropertyInfo propInfo;
          private string _fieldName;
  
          public string fieldName
          {
              get;
              set;
          }
  
          public CompareProperty(string fieldName)
          {
              this.fieldName = fieldName;
          }
  
          public bool Equals&lt;T&gt;(T x, T y)
          {
              if (this.type == null)
              {
                  type = x.GetType();
                  propInfo = type.GetProperty(fieldName);
              }
              object objX = propInfo.GetValue(x, null);
              object objY = propInfo.GetValue(y, null);
              return objX.ToString() == objY.ToString();
          }
      }
  </code></pre>
  
  <p>I thought this was a nice little helper function I could use many times.</p>
  
  <p>In order to use this, I have to do:</p>
  
  <pre><code>var t = typeof(CompareProperty&lt;&gt;);
  var g = t.MakeGenericType(infoType.GetType());
  var c = g.GetConstructor(new Type[] {String.Empty.GetType()});
  var obj = c.Invoke(new object[] {"somePropertyName"});
  </code></pre>
  
  <p>Fair enough, but what do I do with the obj variable it returns?</p>
  
  <pre><code>someEnumerable.Distinct(obj);
  </code></pre>
  
  <p>The overload of the distinct extension function does not accept this, because it does not see a IEqualityComparer type, it only sees an object, of course.</p>
  
  <pre><code>someEnumerable.Distinct((t) obj);
  someEnumerable.Distinct(obj as t);
  </code></pre>
  
  <p>This also doesn't work. Type/Namespace not found (red underline).</p>
  
  <p>How do I get this straight?</p>