Null (?) Operator returns incorrect value when using generic type constructor. VB.NET

廉价感情. 提交于 2020-01-04 15:11:14

问题


I am experiencing some weird behavior when using Generic Types and null operator. Why does obj2.CurrentDate return a date value that appears to be incorrect when using the ? (short hand). If I long hand the null operator (if) in the property then it returns the correct and expected value. I thought the ? was the equivalent to the if(expression, returnIfTrue, returnIfFalse).

Also, if the generic type constructor is removed, then the null operator works as expected. Why is this?

Return If(CurrentObject1 IsNot Nothing, CurrentObject1.MyDate, Nothing) -- Long Hand Returns correct value

Return CurrentObject1?.MyDate --Short Hande - Returns incorrect value

1st Console Application - With Generic Type Constructor, CurrentDate is not what is expected.

Module Module1
    Sub Main()
        Dim obj1 As New MyObject1 With {.MyDate = CDate("2017-11-13T14:25:00Z")}
        Dim obj2 As New MyObject2(Of MyObject1)(obj1)

        Console.WriteLine(obj1.MyDate)
        Console.WriteLine(obj2.CurrentDate)

        Console.ReadKey()
    End Sub
End Module

Public MustInherit Class MyBaseObject1
   Property MyDate As Date
End Class

Public Class MyObject1
    Inherits MyBaseObject1
End Class

Public Class MyObject2(Of MyObjectType As {MyBaseObject1, New})
    Public Sub New(obj As MyObjectType)
        m_CurrentObject1 = obj
    End Sub

    Private m_CurrentObject1 As MyObjectType = Nothing
    Public ReadOnly Property CurrentObject1 As MyObjectType
        Get
            Return m_CurrentObject1
        End Get
    End Property
    Public ReadOnly Property CurrentDate As Date?
        Get
            Return CurrentObject1?.MyDate
        End Get
    End Property
End Class

Output of Console Application 1

11/13/2017 8:25:00 AM

2/24/0010 4:56:53 AM

2nd Console Application - Without generic type constructor. Works as expected

Module Module1
    Sub Main()
        Dim obj1 As New MyObject1 With {.MyDate = CDate("2017-11-13T14:25:00Z")}
        Dim obj2 As New MyObject2(obj1)

        Console.WriteLine(obj1.MyDate)
        Console.WriteLine(obj2.CurrentDate)

        Console.ReadKey()
    End Sub
End Module

Public MustInherit Class MyBaseObject1
    Property MyDate As Date
End Class

Public Class MyObject1
    Inherits MyBaseObject1
End Class

Public Class MyObject2
    Public Sub New(obj As MyBaseObject1)
        m_CurrentObject1 = obj
    End Sub

    Private m_CurrentObject1 As MyBaseObject1 = Nothing
    Public ReadOnly Property CurrentObject1 As MyBaseObject1
        Get
            Return m_CurrentObject1
        End Get
    End Property
    Public ReadOnly Property CurrentDate As Date?
        Get
            Return CurrentObject1?.MyDate
        End Get
    End Property

End Class

Output of 2nd console application

11/13/2017 8:25:00 AM

11/13/2017 8:25:00 AM


回答1:


Yes, this looks like a compiler bug. Will fix it shortly. In the meantime you can use the following workaround: DirectCast(CurrentObject1, MyBaseObject1)?.MyDate



来源:https://stackoverflow.com/questions/47278596/null-operator-returns-incorrect-value-when-using-generic-type-constructor-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!