How to find an object by its hash code?

Is there an easy way to delve into an object and find the property or field of its hashcode? This can be a nested property or a value in a collection. The reason I ask is because I sometimes get WPF warnings that look like this:

System.Windows.ResourceDictionary Warning: 9 : Resource not found;
  ResourceKey='#FF000000'; ResourceKey.HashCode='51639504';
  ResourceKey.Type='System.Windows.Media.SolidColorBrush' 

A warning does not always appear, and it’s the most difficult for me to track it. I believe that if I knew which object has this hash code, I could come close to fixing it. For example, if I had this object:

var first = new { a = 1, b = 2, d = new { aa = 11, bb = 22 } };

and called it on it:

string str = FindHashCode(first, 22);

the result will be:

"Anon > d > bb.hashcode = 22"

or something similar. (Now I ignore the hashcode collision)


: @Alberto. , , . IEnumerables (, ..) , , . -. -, StringBuilder .

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 + " > " + field.Item2 + ".hashcode = " + hashCode);
        }
        else
        {
            FindHashCode(field.Item1, hashCode, path + " > " + field.Item2, strb);
        }
    }
}

static IEnumerable<Tuple<object, string>> GetFieldInfo(object arg)
{
    var ienum = arg as System.Collections.IEnumerable;
    var idict = arg as System.Collections.IDictionary;

    if (ienum == null && idict == null)
    {
        BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
        Type type = arg.GetType();

        var list = type.GetFields(bf).Select(s => new Tuple<object, string>(s.GetValue(arg), s.Name)).Concat(
            type.GetProperties(bf).Select(s => new Tuple<object, string>(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<object, string>(item.Key, string.Format("Dict[{0}].Key", item.Key));
            yield return new Tuple<object, string>(item.Value, string.Format("Dict[{0}].Value", item.Key));
        }
    }

    //note that dictionaries implement IEnumerable
    else if (ienum != null && !(ienum is string)) 
    {
        int count = 0;
        foreach (var item in ienum)
        {
            yield return new Tuple<object, string>(item, string.Format("this[{0}]", count));
            count++;
        }
    }
}
+4
2

, -:

static string FindHashCode(object o, int hashCode)
{
    return FindHashCodeImpl(o,hashCode, o.GetType().Name);
}

static string FindHashCodeImpl(object o, int hashCode, string partialPath)
{
    var type = o.GetType();
    var properties = type.GetProperties();
    foreach (var property in properties)
    {
        var propValue = property.GetValue(o);
        if (propValue.GetHashCode() == hashCode)
        {
            return partialPath + " > " + property.Name + ".hashcode = " + hashCode;
        }

        var path = FindHashCodeImpl(propValue, hashCode, partialPath + " > " + property.Name);
        if (path != null)
        {
            return path;
        }
    }
    return null;
}

:

var o = new { a = 1, b = 2, d = new { aa = 11, bb = 22 } };

var s = FindHashCode(o, 22); //Output "<>f__AnonymousType1`3 > d > bb.hashcode = 22"

.

P.S , ...

+2

, - . (, List), GetHashCode() ( ). , , , - , , ?

Visual Studio , , , , .

, .

Trace WPF System.Windows.ResourceDictionary

+1

Source: https://habr.com/ru/post/1530458/


All Articles