title: Implement Equals to make either reference equality or key-based equality?
body: <p>I would like to override the <code>Equals</code> and <code>GetHashCode</code> of my EntityBase class to support checking equality based on reference (as by default) or by the entity key (in case the references not matching).
  Here is the code:</p>
  
  <pre><code>public abstract class EntityBase
  {
      protected virtual object Keys { get { return this; } }
      public override bool Equals(object obj)
      {
          if (Keys == this) return base.Equals(obj);
          var entity = obj as EntityBase;
          if (entity == null) return false;
          var re = ReferenceEquals(entity, this);
          return re || Equals(entity.Keys, Keys);
      }
      public override int GetHashCode()
      {
          if (Keys == this) return base.GetHashCode();
          return base.GetHashCode() * 17 + (Keys?.GetHashCode() ?? 0);
      }
  }
  </code></pre>
  
  <p>Now in the derived class it can be like this:</p>
  
  <pre><code>public class Entity : EntityBase {
      protected override object Keys {
          get {
               return SomeKeyProperty;
          }
      }
  }
  </code></pre>
  
  <p>So I expect that it should work, but the <code>BindingSource</code> I'm using showed that it does not work, like this:</p>
  
  <pre><code>//the myEntity here is contained (as reference) in myBindingSource  
  var index = myBindingSource.IndexOf(myEntity);
  </code></pre>
  
  <p>The code above gives the correct result if I don't override <code>Equals</code> for my <code>EntityBase</code> class, but with that overriding, the result will be wrong, looks like it always tries to look for the item based on the <code>Keys</code> value. I don't really understand what's wrong here.</p>
  
  <p>When debugging it does not even hit the breakpoint I set in the <code>Equals</code> method. The code just runs through the <code>IndexOf</code> call like as it's a black box.</p>
  
  <p>Could you explain to me what's wrong here and give me some suggestion about a possible fix (or even let me know if what I want to achieve is possible).</p>