我希望能够动态地转换一个值,其中类型仅在运行时已知。像这样的东西:
myvalue = CType(value, "String, Integer or Boolean")
包含类型值的字符串作为参数传递,也从数据库中读取,并将该值作为字符串存储在数据库中。
这可能吗?
最佳答案
Dim bMyValue As Boolean
Dim iMyValue As Integer
Dim sMyValue As String
Dim t As Type = myValue.GetType
Select Case t.Name
Case "String"
sMyValue = ctype(myValue, string)
Case "Boolean"
bMyValue = ctype(myValue, boolean)
Case "Integer"
iMyValue = ctype(myValue, Integer)
End Select
这有点hacky,但它有效。
关于vb.net - 在 VB.NET 中进行转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/251482/