If T
is a Date
and you do
Return Me.Value.Equals(other)
the other
is passed as Object
to the Date.Equals
method which looks like this:
Public Overrides Function Equals(ByVal value As Object) As Boolean
If TypeOf value Is DateTime Then
Dim time As DateTime = CDate(value)
Return (Me.InternalTicks = time.InternalTicks)
End If
Return False
End Function
And as you can see the first condition will return False
.
Dim isdate As Boolean = (TypeOf CObj(New MyWrapperType(Of Date)(Date.MaxValue)) Is Date)
To ensure correct casting you can do something like this:
Public Overrides Function Equals(other As Object) As Boolean
If (TypeOf other Is MyWrapperType(Of T)) Then
Dim obj As MyWrapperType(Of T) = DirectCast(other, MyWrapperType(Of T))
'...Me.Value.Equals(obj.Value)
ElseIf (TypeOf other Is T) Then
Dim obj As T = DirectCast(other, T)
'...Me.Value.Equals(obj)
End If
Return False
End Function
Edit
If we disassemble the Assert.AreEqual
method it looks like this:
Call 1 : Assert
Public Shared Sub AreEqual(ByVal expected As Object, ByVal actual As Object)
Assert.AreEqual(expected, actual, String.Empty, Nothing)
End Sub
Call 2 : Assert
Public Shared Sub AreEqual(ByVal expected As Object, ByVal actual As Object, ByVal message As String, ByVal ParamArray parameters As Object())
Assert.AreEqual(Of Object)(expected, actual, message, parameters)
End Sub
Call 3 : Assert
Public Shared Sub AreEqual(Of T)(ByVal expected As T, ByVal actual As T, ByVal message As String, ByVal ParamArray parameters As Object())
If Not Object.Equals(expected, actual) Then
Dim str As String
If (((Not actual Is Nothing) AndAlso (Not expected Is Nothing)) AndAlso Not actual.GetType.Equals(expected.GetType)) Then
str = CStr(FrameworkMessages.AreEqualDifferentTypesFailMsg(IIf((message Is Nothing), String.Empty, Assert.ReplaceNulls(message)), Assert.ReplaceNulls(expected), expected.GetType.FullName, Assert.ReplaceNulls(actual), actual.GetType.FullName))
Else
str = CStr(FrameworkMessages.AreEqualFailMsg(IIf((message Is Nothing), String.Empty, Assert.ReplaceNulls(message)), Assert.ReplaceNulls(expected), Assert.ReplaceNulls(actual)))
End If
Assert.HandleFail("Assert.AreEqual", str, parameters)
End If
End Sub
Call 4 : Object
Public Shared Function Equals(ByVal objA As Object, ByVal objB As Object) As Boolean
Return ((objA Is objB) OrElse (((Not objA Is Nothing) AndAlso (Not objB Is Nothing)) AndAlso objA.Equals(objB)))
End Function
Assert.AreEqual(x, Date.MaxValue) = True
This would end up in this:
New MyWrapperType(Of DateTime)(Date.MaxValue).Equals(Date.MaxValue)
which finally ends up calling your Equals
method:
Public Overrides Function Equals(other As Object) As Boolean
If Me.Value Is Nothing Then Return other Is Nothing <- Pass, Value is Date.MaxValue, not null
If other Is Nothing Then Return False <- Pass, other is Date.MaxValue, not null
Return Me.Value.Equals(other) <- Pass, Value (Date.MaxValue) = other (Date.MaxValue)
End Function
Assert.AreEqual(Date.MaxValue, x) = False
This would end up in this:
Date.MaxValue.Equals(New MyWrapperType(Of DateTime)(Date.MaxValue))
which finally ends up calling your Date.Equals(obj As Object)
method:
Public Overrides Function Equals(ByVal value As Object) As Boolean
If TypeOf value Is DateTime Then '< Fail, value is not a DateTime, it's a MyWrapperType(Of T)
Dim time As DateTime = CDate(value)
Return (Me.InternalTicks = time.InternalTicks)
End If
Return False
End Function