wpf 重写 ContentControl 中的 getHashCode 和 Eqaul
你好 我有一个派生自 ContentControl 的类,我无法重写 GetHashCode 和 Equal 方法。我收到错误
错误 5 无法覆盖继承成员“System.Windows.DependencyObject.GetHashCode()”,因为它是密封的 有什么办法可以重写这个方法吗? 我需要使用 LINQ 中的 Union 方法,但是我需要比较具有与正常条件不同的条件的对象。有什么办法可以做到吗?
Hi
I have a class which derives from ContentControl and I'm not able to override GetHashCode and Equal method. I get an error
Error 5 cannot override inherited member 'System.Windows.DependencyObject.GetHashCode()' because it is sealed
Is there any way to override this method ?
I need to use Union method from LINQ however I need to compare object with different condition than normal. Is there any way to do it ?
是 - 单独实施
IEqualityComparer
,并将其传递到Union
的相关重载。基本上,您将告诉它如何比较任意两项的相等性,以及如何获取任意一项的哈希码。
Union
将在构建哈希集等时使用您的自定义比较。您不需要覆盖任何现有方法。Yes - implement
IEqualityComparer<T>
separately, and pass that into the relevant overload ofUnion
.Basically you'll be telling it how to compare any two items for equality, and how to take the hash code of any one item.
Union
will use your custom comparison when building up hash sets etc. You don't need to override any existing methods.