有条件地检查GetType()s容器中的值类型

时间:2013-07-22 15:09:27

标签: vb.net types compare conditional

我正在填写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...

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

1 个答案:

答案 0 :(得分:3)

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

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框架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