vb.net - 在 VB.NET 中进行转换

标签 vb.net casting ctype

我希望能够动态地转换一个值,其中类型仅在运行时已知。像这样的东西:

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/

相关文章:

c++ - 编译器是否避免中间整数提升或转换?

vb.net - "Access to the file path is denied"

c++ - C++ 如何检查 std::ios 对象的真值?

c# - 将对象转换到 T

c++ - 如何在 C++ 中为 iso-8859-15 创建自定义 ctype

python - python中动态模块的init函数是什么?

c - C 中 isdigit() 的段错误?

regex - 正则表达式帮助 - 逗号分隔字符串

css - VS 2015 - WebBrowser 控件不会显示 CSS 中的交替表行颜色

mysql - 如何使用 Windows 窗体 (vb.net) 将数据库单列行存储到数组中