title: How do I find an object(s) by its hashcode?
body: <p>Is there an easy way to dig in to an object and find a property or field by its hashcode?  This could be a nested property or a value in a collection.  The reason I ask is that I occasionally get WPF warnings that look like:</p>
  
  <pre><code>System.Windows.ResourceDictionary Warning: 9 : Resource not found;
    ResourceKey='#FF000000'; ResourceKey.HashCode='51639504';
    ResourceKey.Type='System.Windows.Media.SolidColorBrush' 
  </code></pre>
  
  <p>The warning doesn't always appear and I'm having the hardest time tracking it down.  I figure if I knew which object had that hashcode, I could get closer to fixing this.  For instance, if I had this object:</p>
  
  <pre><code>var first = new { a = 1, b = 2, d = new { aa = 11, bb = 22 } };
  </code></pre>
  
  <p>and called this on it:</p>
  
  <pre><code>string str = FindHashCode(first, 22);
  </code></pre>
  
  <p>the result would be:</p>
  
  <pre><code>"Anon &gt; d &gt; bb.hashcode = 22"
  </code></pre>
  
  <p>or something similar.  (I'm ignoring hashcode collisions for now)</p>
  
  <hr>
  
  <p>Edit: Here's what I'll use based on @Alberto's answer.  It searches both fields and properties whether public or non-public.  It includes support for IEnumerables (Lists, Arrays, etc.) and more specifically IDictionaries.  It also handles hashcode collisions.  If two objects have the same hashcode, the StringBuilder will have a separate line for each object.</p>
  
  <pre><code>using System.Reflection;
  
  static string FindHashCode(object o, int hashCode)
  {
      StringBuilder strb = new StringBuilder();
      FindHashCode(o, hashCode, o.GetType().Name, strb);
      return strb.ToString().Trim();
  }
  
  static void FindHashCode(object o, int hashCode, string path, StringBuilder strb)
  {
      if (o.GetHashCode() == hashCode)
      {
          strb.AppendLine(path + ".hashcode = " + hashCode);
      }
  
      foreach (var field in GetFieldInfo(o))
      {
          if (field.Item1 == null || object.ReferenceEquals(o, field.Item1))
              continue;
  
          Type type = field.Item1.GetType();
          if (type.IsPrimitive)
          {
              if(field.Item1.GetHashCode() == hashCode)
                  strb.AppendLine(path + " &gt; " + field.Item2 + ".hashcode = " + hashCode);
          }
          else
          {
              FindHashCode(field.Item1, hashCode, path + " &gt; " + field.Item2, strb);
          }
      }
  }
  
  static IEnumerable&lt;Tuple&lt;object, string&gt;&gt; GetFieldInfo(object arg)
  {
      var ienum = arg as System.Collections.IEnumerable;
      var idict = arg as System.Collections.IDictionary;
  
      if (ienum == null &amp;&amp; idict == null)
      {
          BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
          Type type = arg.GetType();
  
          var list = type.GetFields(bf).Select(s =&gt; new Tuple&lt;object, string&gt;(s.GetValue(arg), s.Name)).Concat(
              type.GetProperties(bf).Select(s =&gt; new Tuple&lt;object, string&gt;(s.GetValue(arg, null), s.Name)));
  
          foreach (var item in list)
          {
              yield return item;
          }
      }
      else if (idict != null)
      {
          foreach (System.Collections.DictionaryEntry item in idict)
          {
              yield return new Tuple&lt;object, string&gt;(item.Key, string.Format("Dict[{0}].Key", item.Key));
              yield return new Tuple&lt;object, string&gt;(item.Value, string.Format("Dict[{0}].Value", item.Key));
          }
      }
  
      //note that dictionaries implement IEnumerable
      else if (ienum != null &amp;&amp; !(ienum is string)) 
      {
          int count = 0;
          foreach (var item in ienum)
          {
              yield return new Tuple&lt;object, string&gt;(item, string.Format("this[{0}]", count));
              count++;
          }
      }
  }
  </code></pre>