有没有办法强制VB.Net匿名类型的属性类型?

时间:2018-02-11 19:39:45

标签: vb.net anonymous-types object-initializers

我的意思是这样的(粗体):

Dim anonType = New {{.Property1 = 10,.Property2 As Decimal? = Nothing}

1 个答案:

答案 0 :(得分:0)

你可以随时解决这个问题..

Private Sub test()
    Dim tempDecimal? As Decimal = Nothing
    Dim anonType = New With {.Property1 = 10, .Property2 = tempDecimal}
    ' property value is nothing here, but if tempDecimal isnt declared as nullable,
    'the value will be 0
    anonType.property2 = Nothing 
    anonType.property2 = 5
    'tp will be Decimal here
    Dim tp As Type = anonType.property2.GetType
End Sub