【问题标题】:Null (?) Operator returns incorrect value when using generic type constructor. VB.NETNull (?) 运算符在使用泛型类型构造函数时返回不正确的值。 VB.NET
【发布时间】:2018-04-26 23:27:27
【问题描述】:

我在使用泛型类型和 null 运算符时遇到了一些奇怪的行为。为什么 obj2.CurrentDate 在使用时返回的日期值似乎不正确? (短手)。如果我将属性中的 null 运算符 (if) 长时间交给它,那么它会返回正确的预期值。我以为?相当于 if(expression, returnIfTrue, returnIfFalse)。

此外,如果删除了泛型类型构造函数,则 null 运算符将按预期工作。这是为什么呢?

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

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

第一个控制台应用程序 - 使用通用类型构造函数,CurrentDate 不是预期的。

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

控制台应用程序 1 的输出

2017 年 11 月 13 日上午 8:25:00

2/24/0010 凌晨 4:56:53

第二个控制台应用程序 - 没有泛型类型构造函数。按预期工作

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

第二个控制台应用程序的输出

2017 年 11 月 13 日上午 8:25:00

2017 年 11 月 13 日上午 8:25:00

【问题讨论】:

  • 您应该将它输出的值编辑到问题中 - 这将更有可能激起某人的兴趣。我得到“24/02/0010 04:56:53”的代码,如果我使用Property MyDate As Date?,我得到“01/01/0001 00:28:38”
  • 请记住,VB 的Nothing 更类似于C# 的default(T),而不是null。因此,例如,您的第一个表达式的 type 是 Date,not Date?。
  • 您发现了一个编译器错误,它没有为CurrentObject1?.MyDate 表达式生成正确的代码。它生成了错误的 MSIL 代码,当您在测试程序上运行 peverify.exe 时可以看到这些代码。点击New Issue button 让他们知道。
  • 同时,将Return If(CurrentObject1 Is Nothing, Nothing, CurrentObject1.MyDate) 视为一种解决方法。

标签: .net vb.net date generics nullable


【解决方案1】:

是的,这看起来像是一个编译器错误。很快就会修复它。同时,您可以使用以下解决方法: DirectCast(CurrentObject1, MyBaseObject1)?.MyDate

【讨论】:

  • 这个 bug is here 的另一个受害者。我想知道 Roslyn 问题可能来自哪里,看起来用户 #8982039 实际上是 AlekseyTs。
猜你喜欢
  • 2019-05-04
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 2015-06-18
  • 1970-01-01
  • 1970-01-01
  • 2019-02-28
  • 2022-01-19
相关资源
最近更新 更多