【问题标题】:Obtain the Type of a Nullable Property that has a Nothing value获取具有 Nothing 值的 Nullable 属性的类型
【发布时间】:2013-06-02 16:03:24
【问题描述】:

我正在 VB.NET(框架 3.5)中尝试获取具有 Nothing 值的 Nullable 属性的类型(其默认值),以了解如何制作 CType。代码是这样的:

Class DinamicAsign
Public Property prop As Integer?
Public Property prop2 As Date?

Public Sub New()
    Asign(prop, "1")
    Asign(prop2, "28/05/2013")
End Sub

Public Sub Asign(ByRef container As Object, value As String)
    If (TypeOf (container) Is Nullable(Of Integer)) Then
        container = CType(value, Integer)
    ElseIf (TypeOf (container) Is Nullable(Of Date)) Then
        container = CType(value, Date)
    End If
End Sub
End Class

此代码无法正常工作。问题是如何知道“容器”的类型。

如果 "prop" 有一个值("prop" 不是什么),则此代码有效:

If (TypeOf(contenedor) is Integer) then...
If (contenedor.GetType() is Integer) then...

但是如果值什么都没有,我不知道如何获取类型。我已经尝试过这种方法,但不起作用:

container.GetType() 

TypeOf (contenedor) is Integer 

TypeOf (contenedor) is Nullable(of Integer) 

我知道有人可能会回应说“容器”什么都不是,因为它没有引用任何对象,而且您不知道类型。但这似乎是错误的,因为我找到了解决这个问题的技巧:创建一个重载函数来进行强制转换,这样:

Class DinamicAsign2
Public Property prop As Integer?
Public Property prop2 As Date?

Public Sub New()
    Asignar(prop, "1")
    Asignar(prop2, "28/05/2013")
End Sub

Public Sub Asignar(ByRef container As Object, value As String)
    AsignAux(container, value)
End Sub

Public Sub AsignAux(ByRef container As Integer, value As String)
    container = CType(value, Integer)
End Sub

Public Sub AsignAux(ByRef container As Decimal, value As String)
    container = CType(value, Decimal)
End Sub
End Class

如果“容器”是整数,它会调用到

public function AsignAux(byref container as Integer, value as string)

如果 "container" Is Date 会调用到

public function AsignAux(byref container as Date, value as string)

这很好用,.NET 无论如何都知道 Object 的类型,因为调用正确的重载函数。 所以我想找到(就像 .NET 一样)一种方法来确定具有空值的 Nullable 对象的类型

谢谢

【问题讨论】:

    标签: .net vb.net reflection types casting


    【解决方案1】:

    Nullable(Of T) 变成Object 时,类型数据会丢失:它要么变成普通的旧Nothing,要么变成它所代表的类型,例如Integer。你也许可以改变你的方法来做到这一点:

    Public Sub Asign(Of T As Structure)(ByRef container As Nullable(Of T), value As String)
        ' T is Integer or Date, in your examples
        container = System.Convert.ChangeType(value, GetType(T))
    End Sub
    

    如果没有,您必须在别处记录类型,并将其传递到您的方法中。

    有关为什么设置装箱/拆箱以这种方式工作的一些信息,请参阅Boxing / Unboxing Nullable Types - Why this implementation?。简而言之,将可空类型用作Object 是最明智的方式。

    【讨论】:

    • 完美解决方案,非常感谢!无论如何,我认为当您使用对象参数时,类型不会丢失,因为正如我在之前的代码中所说,如果您使用重载函数,.NET 会根据对象参数的类型(即取决于类型)知道哪个函数调用可空属性)
    • 嗯,有趣的是,显然 VB 编译器或运行时做了一些 C#(我更熟悉的)没有的“魔法”,它允许您在 DinamicAsign2 中的代码工作。除了可以为空之外,在 C# 中,如果您在 C# 中有一个 Object 并且您尝试以这种方式调用 AsignAux,它将无法解析。
    • 如果你有一个 Nullable(Of Integer) 的值,也就是 Nothing,并且你把它放在一个 Object 变量中,你会在调试器的任何地方看到诸如 Integer 这样的类型吗对于那个Object?在 C# 中你不会。但是,如果您在 C# 中有一个具有真正 int 值的 int?,并且您将其存储在一个 object 中,它就变成了一个简单的 int(不再是 int?)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 2020-09-16
    • 1970-01-01
    相关资源
    最近更新 更多