0

だから私はこの(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

大きな謎は、データベースからオブジェクトをロードすると、 inの値が正しいにもかかわらず、 in が常にゼロになることですEquals()other.fooFoo()

これはどうやってできるの?

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.fooother.foo

4

1 に答える 1

0

数時間後、オブジェクト タイプの Equals() メソッドが原因であることがわかりました。おそらく、DirectCast で処理されたインスタンスがプロキシをバイパスしたためです。

2 番目のインスタンスの問題は、プライベートな実装 (インターフェイスの! 誰が知っていた!) がプロキシをバイパスしていたことです。

したがって、この話の教訓は、DirectCast避け、プライベート インターフェイスの実装を避けることです。 :-)

于 2013-01-29T02:35:27.330 に答える