空(?)运算符在使用泛型类型构造函数时返回不正确的值。 VB.NET [英] Null (?) Operator returns incorrect value when using generic type constructor. VB.NET

查看:103
本文介绍了空(?)运算符在使用泛型类型构造函数时返回不正确的值。 VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用泛型类型和空操作符时遇到了一些奇怪的行为。为什么obj2.CurrentDate在使用?时返回一个看起来不正确的日期值? (短手)。如果我长时间在属性中传递null操作符(if),那么它会返回正确的和期望的值。我以为?等价于if(expression,returnIfTrue,returnIfFalse)。

另外,如果泛型类型的构造函数被删除,那么null操作符按预期工作。这是为什么?

 返回If(CurrentObject1 IsNot Nothing,CurrentObject1.MyDate,Nothing) -  Long Hand返回正确值

返回CurrentObject1?.MyDate --Short Hande - 返回不正确的值

第一个控制台应用程序 - 使用泛型类型构造函数,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

公共MustInherit类MyBaseObject1
属性MyDate作为日期
结束类

公共类MyObject1
继承MyBaseObject1
结束类

Public Class MyObject2(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
返回m_CurrentObject1
结束获取
结束属性
公共ReadOnly属性CurrentDate As Date?
Get
返回CurrentObject1?.MyDate
End
End Property
End Class

控制台应用程序的输出1



11/13/2017 8:25:00 AM $ b

2/24/0010 4:56:53 AM

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

  Module Module1 
Sub Main()
Dim obj1 As New 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

公共MustInherit类MyBaseObject1
属性MyDate As日期
结束类

公共类MyObject1
继承MyBaseObject1
结束类

公共类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
返回m_CurrentObject1
结束获取
结束属性
公共ReadOnly属性CurrentDate As Date?
Get
返回CurrentObject1?.MyDate
End
End Property

End Class

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

11/13/2017 8:25:00 AM



11/13/2017 8:25:00 AM

解决方案

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


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

解决方案

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

这篇关于空(?)运算符在使用泛型类型构造函数时返回不正确的值。 VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆