gpt4 book ai didi

c# - HashSet.Contains 和自定义 IEqualityComparer 的意外结果

转载 作者:行者123 更新时间:2023-11-30 15:58:36 35 4
gpt4 key购买 nike

对于将自定义比较器与 HashSet 结合使用,我一定有某种误解。我收集了许多不同类型的数据,中间存储为 Json。为了对其进行操作,我使用了 Json.NET,特别是 JObjectJArrayJtoken

通常我会在收集时向这些内容添加一些内联元数据,并以“tbp_”为前缀。我需要知道表示为 JObject 的特定数据位是否已被收集(或未被收集)。为此,我有一个自定义 IEqualityComparer,它扩展了 Json.NET 提供的实现。它在使用提供的实现检查值是否相等之前去除元数据:

public class EntryComparer : JTokenEqualityComparer
{
private static string _excludedPrefix = "tbp_";

public JObject CloneForComparison(JObject obj)
{
var clone = obj.DeepClone() as JObject;
var propertiesToRemove = clone
.Properties()
.Where(p => p.Name.StartsWith(_excludedPrefix));

foreach (var property in propertiesToRemove)
{
property.Remove();
}

return clone;
}

public bool Equals(JObject obj1, JObject obj2)
{
return base.Equals(CloneForComparison(obj1), CloneForComparison(obj2));
}

public int GetHashCode(JObject obj)
{
return base.GetHashCode(CloneForComparison(obj));
}
}

我使用 HashSet 来跟踪我正在操作的数据,因为我只需要知道它是否已经存在。我使用 EntryComparer 的实例初始化 HashSet。我的测试是:

public class EntryComparerTests
{
EntryComparer comparer;
JObject j1;
JObject j2;

public EntryComparerTests()
{
comparer = new EntryComparer();
j1 = JObject.Parse(@"
{
'tbp_entry_date': '2017-03-25T21:25:53.127993-04:00',
'from_date': '1/6/2017',
'to_date': '2/7/2017',
'use': '324320',
'reading': 'act',
'kvars': '0.00',
'demand': '699.10',
'bill_amt': '$28,750.75'
}");
j2 = JObject.Parse(@"
{
'tbp_entry_date': '2017-03-10T18:59:00.537745-05:00',
'from_date': '1/6/2017',
'to_date': '2/7/2017',
'use': '324320',
'reading': 'act',
'kvars': '0.00',
'demand': '699.10',
'bill_amt': '$28,750.75'
}");
}

[Fact]
public void Test_Equality_Comparer_GetHashCode()
{
Assert.Equal(comparer.GetHashCode(j1), comparer.GetHashCode(j2));
Assert.Equal(true, comparer.Equals(j1, j2));
}

[Fact]
public void Test_Equality_Comparer_Hashset_Contains()
{
var hs = new HashSet<JObject>(comparer);
hs.Add(j1);

Assert.Equal(true, hs.Contains(j2));
}
}

Test_Equality_Comparer_GetHashCode() 通过,但 Test_Equality_Comparer_Hashset_Contains() 失败。 j1j2 应该被视为相等,并且根据第一次测试的结果,那么我在这里缺少什么?

最佳答案

更改类的签名:

public class EntryComparer : JTokenEqualityComparer, IEqualityComparer<JObject>

否则 GetHashCode()Equals()使用的是基类中的那些(具有不同的“签名”......基类实现了 IEqualityComparer<JToken> ,因此你的方法不会被 HashSet<> 调用)。

然后有一个属性删除的小错误:

var propertiesToRemove = clone
.Properties()
.Where(p => p.Name.StartsWith(_excludedPrefix))
.ToArray();

更好的做法是“隐藏”JTokenEqualityComparer并将其设为私有(private)字段,例如:

public class EntryComparer : IEqualityComparer<JObject>
{
private static readonly JTokenEqualityComparer _comparer = new JTokenEqualityComparer();
private static readonly string _excludedPrefix = "tbp_";

public static JObject CloneForComparison(JObject obj)
{
var clone = obj.DeepClone() as JObject;
var propertiesToRemove = clone
.Properties()
.Where(p => p.Name.StartsWith(_excludedPrefix))
.ToArray();

foreach (var property in propertiesToRemove)
{
property.Remove();
}

return clone;
}

public bool Equals(JObject obj1, JObject obj2)
{
return _comparer.Equals(CloneForComparison(obj1), CloneForComparison(obj2));
}

public int GetHashCode(JObject obj)
{
return _comparer.GetHashCode(CloneForComparison(obj));
}
}

关于c# - HashSet.Contains 和自定义 IEqualityComparer 的意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43071177/

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