gpt4 book ai didi

vb.net - 有条件地检查 GetType() 容器中的值类型

转载 作者:行者123 更新时间:2023-12-01 01:50:04 25 4
gpt4 key购买 nike

我正在填写 this answer 中指定的 List(Of Type)例如

Dim columnTypes = New List(Of Type) From {GetType(Integer), GetType(Integer),
GetType(String), GetType(String)}

我想将 List 与实际的 Type 关键字进行比较,例如伪造的 String:

If columnTypes(i) = String Then
//do it because it's a String and not an Integer or Boolean or Object or...

如何有条件地确定此List 中的Type

最佳答案

不要使用它的字符串表示,但是:

If columnTypes(0) = GetType(String) Then
Console.Write("I'm a string")
End If

或者相反

If columnTypes.Contains(GetType(String)) Then
Console.Write("It contains a string")
End If

自 NET framework 4 以来,类型比较以这种方式工作,以前的版本使用 Is :

If columnTypes(0) Is GetType(String) Then
  Console.Write("I'm a string")
ElseIf columnTypes(0) Is GetType(Int32) Then
Console.Write("I'm an integer")
End If

关于vb.net - 有条件地检查 GetType() 容器中的值类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17791085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com