[英]Why does overriding GetHashCode() block INotifyPropertyChanged?
I have a dead simple view model that inherits from INotifyPropertyChanged
. 我有一个死掉的简单视图模型,该模型继承自
INotifyPropertyChanged
。 After attempting to create some automated tests for this view model, I ended up overriding Equals()
and GetHashCode()
. 在尝试为此视图模型创建一些自动化测试之后,我最终重写了
Equals()
和GetHashCode()
。 My tests passed, so I was a happy camper. 我的考试通过了,所以我是个快乐的露营者。
However, now my Windows Forms data bindings aren't reacting to when a property in my view model changes. 但是,现在,当视图模型中的属性发生更改时,我的Windows Forms数据绑定没有反应。 If I comment my
GetHashCode()
implementation all works as expected, except for my tests. 如果我评论我的
GetHashCode()
实现,除了我的测试,所有其他工作都按预期进行。
Here is my implementation: 这是我的实现:
public override int GetHashCode()
{
unchecked
{
var hashCode = (_subject != null ? _subject.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (_message != null ? _message.GetHashCode() : 0);
hashCode = (hashCode * 397) ^
(_body != null ? _body.GetHashCode() : 0);
return hashCode;
}
}
This is simply what ReSharper generated for me. 这就是ReSharper为我生成的。
Why does overriding GetHashCode()
stop my data bindings from picking up on my view model changes despite me calling the PropertyChanged
event? 为什么尽管我调用了
PropertyChanged
事件,但是重写GetHashCode()
阻止了我的数据绑定发生在视图模型更改中?
I always try to avoid adding code to production code that is solely for testing purposes. 我总是尽量避免将代码添加到仅用于测试目的的生产代码中。 So since you implemented
Equals
and GetHashCode
for testing purposes, would it be possible to implement IEqualityComparer<T>
and use that in your unit tests to performs the assertions. 因此,由于出于测试目的而实现了
Equals
和GetHashCode
,因此有可能实现IEqualityComparer<T>
并在单元测试中使用它来执行断言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.