gpt4 book ai didi

c# - 我如何通过哈希码找到一个对象?

转载 作者:太空狗 更新时间:2023-10-29 23:22:55 27 4
gpt4 key购买 nike

有没有一种简单的方法可以深入研究一个对象并通过其哈希码找到一个属性或字段?这可以是嵌套属性或集合中的值。我问的原因是我偶尔会收到如下所示的 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 的回答使用的内容。它搜索公共(public)或非公共(public)的字段和属性。它包括对 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++;
}
}
}

最佳答案

这里是一个在对象图中递归搜索具有特定哈希码的属性的实现:

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 我没有针对每种情况进行测试,但它应该可以工作...

关于c# - 我如何通过哈希码找到一个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22231010/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com