use*_*861 5 c# reflection hashcode
有没有一种简单的方法可以深入到对象并通过其哈希码查找属性或字段?这可以是嵌套属性或集合中的值。我问的原因是,我偶尔会收到如下WPF警告:
System.Windows.ResourceDictionary Warning: 9 : Resource not found;
ResourceKey='#FF000000'; ResourceKey.HashCode='51639504';
ResourceKey.Type='System.Windows.Media.SolidColorBrush'
Run Code Online (Sandbox Code Playgroud)
警告并不总是出现,而我很难找到它。我知道如果我知道哪个对象具有该哈希码,那么我可以更进一步地解决这个问题。例如,如果我有这个对象:
var first = new { a = 1, b = 2, d = new { aa = 11, bb = 22 } };
Run Code Online (Sandbox Code Playgroud)
并对此进行了调用:
string str = FindHashCode(first, 22);
Run Code Online (Sandbox Code Playgroud)
结果将是:
"Anon > d > bb.hashcode = 22"
Run Code Online (Sandbox Code Playgroud)
或类似的东西。(我现在暂时忽略哈希码冲突)
编辑:这是基于@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++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
下面是在对象图中递归搜索具有特定哈希码的属性的实现:
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;
}
Run Code Online (Sandbox Code Playgroud)
像这样使用它:
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"
Run Code Online (Sandbox Code Playgroud)
您还应该将其扩展为在字段内进行搜索。
PS我没有对每个场景进行测试,但它应该有效......
| 归档时间: |
|
| 查看次数: |
1436 次 |
| 最近记录: |