如何通过哈希码找到对象?

时间:2014-03-06 16:57:19

标签: c# reflection hashcode

是否有一种简单的方法可以挖掘对象并通过其哈希码查找属性或字段?这可以是嵌套属性或集合中的值。我问的原因是我偶尔会收到WPF警告:

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

警告并不总是出现,而且我最难跟踪它。我想如果我知道哪个对象有该哈希码,我可以更接近解决这个问题。例如,如果我有这个对象:

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

并在上面调用它:

string str = FindHashCode(first, 22);

结果将是:

"Anon > d > bb.hashcode = 22"

或类似的东西。 (我现在忽略哈希码冲突)


编辑:这是我将根据@ Alberto的回答使用的内容。它搜索公共或非公共领域和属性。它包括对IEnumerables(列表,数组等)的支持,更具体地说是IDictionaries。它还处理哈希码冲突。如果两个对象具有相同的哈希码,则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++;
        }
    }
}

2 个答案:

答案 0 :(得分: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我没有为每个场景测试它,但它应该有用......

答案 1 :(得分:1)

对于哈希码与特定值匹配的成员,没有自动搜索对象图的方法。但是如果您知道结构(如List),那么您可以为每个结构和GetHashCode()返回并返回匹配(其中可能有很多)。我认为你必须对你正在使用的数据有一些概念,一些容器如列表或树,对吗?

Visual Studio调试器还允许您将对象ID分配给监视中的项目,以便您可以知道两个引用属于同一项目,以防有用。

我认为有更快的方法可以找到您的错误。

Trace WPF System.Windows.ResourceDictionary warning Resource not found