标签:gethashcode equals c
为了进行单元测试,我经常不得不重写Equals和GetHashCode方法.之后,我的课程开始看起来像这样:
public class TestItem
{
public bool BoolValue { get; set; }
public DateTime DateTimeValue { get; set; }
public double DoubleValue { get; set; }
public long LongValue { get; set; }
public string StringValue { get; set; }
public SomeEnumType EnumValue { get; set; }
public decimal? NullableDecimal { get; set; }
public override bool Equals(object obj)
{
var other = obj as TestItem;
if (other == null)
{
return false;
}
if (object.ReferenceEquals(this, other))
{
return true;
}
return this.BoolValue == other.BoolValue
&& this.DateTimeValue == other.DateTimeValue
&& this.DoubleValue == other.DoubleValue // that's not a good way, but it's ok for demo
&& this.EnumValue == other.EnumValue
&& this.LongValue == other.LongValue
&& this.StringValue == other.StringValue
&& this.EnumValue == other.EnumValue
&& this.NullableDecimal == other.NullableDecimal;
}
public override int GetHashCode()
{
return this.BoolValue.GetHashCode()
^ this.DateTimeValue.GetHashCode()
^ this.DoubleValue.GetHashCode()
^ this.EnumValue.GetHashCode()
^ this.LongValue.GetHashCode()
^ this.NullableDecimal.GetHashCode()
^ (this.StringValue != null ? this.StringValue.GetHashCode() : 0);
}
}
尽管这样做并不难,但是在Equals和GetHashCode中维护相同字段的列表会无聊又容易出错.有没有办法只列出一次用于相等性检查和哈希码功能的fileld? Equals和GetHashCode应该根据此设置列表来实现.
在我的想象中,此类设置列表的配置和用法可能看起来像
public class TestItem
{
// same properties as before
private static readonly EqualityFieldsSetup Setup = new EqualityFieldsSetup<TestItem>()
.Add(o => o.BoolValue)
.Add(o => o.DateTimeValue)
// ... and so on
// or even .Add(o => o.SomeFunction())
public override bool Equals(object obj)
{
return Setup.Equals(this, obj);
}
public override int GetHashCode()
{
return Setup.GetHashCode(this);
}
}
有一种方法可以自动实现hashCode并在Java中等于project lombok.我想知道是否有任何目的可以减少可用于C#的样板代码.
解决方法:
我进行了一些研究,发现一些并非我想要的组件:
> EqualityComparer(nuget)-默认情况下似乎并没有提供有意义的GetHashCode(),并且太重了我的口味.
> AnonymousComparer(nuget)-不支持GetHashCode()组成.
> MemberwiseEqualityComparer-需要添加自定义属性以将成员从比较中排除,这样就无法灵活地为现有类型配置比较.亲自执行此任务的发射有点矫kill过正.
> System.DataStructures.FuncComparer(nuget)-不支持合成.
还有一些相关的讨论:
> Hows to quick check if data transfer two objects have equal properties in C#?
> Is there a better way to implment Equals for object with lots of fields?
到目前为止,明确配置成员列表的想法似乎很独特.并且我实现了自己的库https://github.com/alabax/YetAnotherEqualityComparer.它比TylerOhlsen建议的代码更好,因为它不对提取的成员进行装箱,并且使用EqualityComparer< T>.比较成员.
现在的代码如下:
public class TestItem
{
private static readonly MemberEqualityComparer<TestItem> Comparer = new MemberEqualityComparer<TestItem>()
.Add(o => o.BoolValue)
.Add(o => o.DateTimeValue)
.Add(o => o.DoubleValue) // IEqualityComparer<double> can (and should) be specified here
.Add(o => o.EnumValue)
.Add(o => o.LongValue)
.Add(o => o.StringValue)
.Add(o => o.NullableDecimal);
// property list is the same
public override bool Equals(object obj)
{
return Comparer.Equals(this, obj);
}
public override int GetHashCode()
{
return Comparer.GetHashCode(this);
}
}
同样,MemberEqualityComparer实现IEqualityComparer< T>.并遵循其语义:它可以成功比较default(T)(对于引用类型和Nullables可以为null).
更新:有些工具可以解决创建基于成员的IEqualityComparer< T>的相同问题.而且这些还可以提供复合IComparer< T> ;!
> Comparers (by Stephen Cleary)(nuget).
> ComparerExtensions(nuget).
标签:gethashcode,equals,c 来源: https://codeday.me/bug/20191123/2064667.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。