所以我有这个(VB,对不起)对象:
Class Foo
Private ReadOnly foo as Integer
Public Overridable ReadOnly Property Foo() as Integer
Get
Return foo
End Get
End Property
Public Overridable Overloads Function Equals(ByVal other as Foo) as Boolean
Return Me.foo.Equals(other.foo)
End Function
Public Overloads Overrides Function Equals(ByVal obj as Object) as Boolean
... some boilerplate ...
Return Equals(DirectCast(obj, Foo))
End Function
End Class
最大的谜团是,当我从数据库中加载一个对象时, inEquals()
始终other.foo
为零,即使 in 的值Foo()
是正确的。
这怎么可能?
Equals 方法的另一个版本是这样的:
Private Overloads Function Equals(ByVal other as Foo) as Boolean Implements IEquatable(Of Foo).Equals
Return Me.foo.Equals(other.foo)
End Function
在这个版本中,和都 为零。Me.foo
other.foo