如何检查对象是否是某种类型

Lea*_*eah 88 .net vb.net object object-type drop-down-menu

我将各种对象传递给子例程以运行相同的进程,但每次使用不同的对象.例如,在一种情况下,我使用ListView,在另一种情况下,我传递DropDownList.

我想检查传递的对象是否是DropDownList,然后执行一些代码.我该怎么做呢?

到目前为止我的代码不起作用:

Sub FillCategories(ByVal Obj As Object)
    Dim cmd As New SqlCommand("sp_Resources_Categories", Conn)
    cmd.CommandType = CommandType.StoredProcedure
    Obj.DataSource = cmd.ExecuteReader
    If Obj Is System.Web.UI.WebControls.DropDownList Then

    End If
    Obj.DataBind()
End Sub
Run Code Online (Sandbox Code Playgroud)

Cod*_*ray 145

在VB.NET中,您需要使用该GetType方法来检索对象实例的类型,并使用GetType()运算符来检索另一种已知类型的类型.

一旦有了这两种类型,您只需使用Is运算符进行比较即可.

所以你的代码实际上应该像这样编写:

Sub FillCategories(ByVal Obj As Object)
    Dim cmd As New SqlCommand("sp_Resources_Categories", Conn)
    cmd.CommandType = CommandType.StoredProcedure
    Obj.DataSource = cmd.ExecuteReader
    If Obj.GetType() Is GetType(System.Web.UI.WebControls.DropDownList) Then

    End If
    Obj.DataBind()
End Sub
Run Code Online (Sandbox Code Playgroud)

您也可以使用TypeOf运算符而不是GetType方法.请注意,这将测试您的对象是否与给定类型兼容,而不是它是相同的类型.这看起来像这样:

If TypeOf Obj Is System.Web.UI.WebControls.DropDownList Then

End If
Run Code Online (Sandbox Code Playgroud)

完全琐碎,无关紧要的挑剔:传统上,在编写.NET代码(VB.NET或C#)时,参数的名称是camelCased(这意味着它们总是以小写字母开头).这使得它们很容易一目了然地与类,类型,方法等区分开来.

  • 这两者之间有一个重要的区别,这就是我发表这篇文章的原因.如果对象属于从您正在检查的类型继承的类,则TypeOf检查将返回True,而GetType仅在完全相同的类时才返回True. (34认同)

Ama*_*Ama 5

与科迪·格雷的回应有关的更多细节。由于我花了一些时间来消化它,我认为它可能对其他人有用。

首先,一些定义:

  1. 有 TypeName,它们是对象、接口等类型的字符串表示形式。例如,Bar是 TypeName in Public Class Bar、 或 in Dim Foo as Bar。TypeNames 可以被视为代码中使用的“标签”,用于告诉编译器在描述所有可用类型的字典中查找哪个类型定义。
  2. 有些System.Type对象包含一个值。该值表示一个类型;就像 aString会接受一些文本或 anInt会接受一个数字一样,只不过我们存储的是类型而不是文本或数字。Type对象包含类型定义及其相应的 TypeName。

二、理论:

  1. Foo.GetType()返回一个Type包含变量类型的对象Foo。换句话说,它告诉您什么Foo是 的实例。
  2. GetType(Bar)返回一个Type包含 TypeName 类型的对象Bar
  3. 在某些情况下,对象所经历的类型Cast与对象最初实例化的类型不同。在以下示例中,MyObj 被Integer强制转换为Object

    Dim MyVal As Integer = 42 Dim MyObj As Object = CType(MyVal, Object)

那么,是MyObj属于类型Object还是属于类型IntegerMyObj.GetType()会告诉你这是一个Integer.

  1. 但这里有一个Type Of Foo Is Bar功能,它允许您确定变量Foo与 TypeName 兼容BarType Of MyObj Is Integer并且 Type Of MyObj Is Object都会返回 True。对于大多数情况,如果变量属于该类型或派生自该类型的类型,则 TypeOf 将指示该变量与 TypeName 兼容。更多信息请参见:https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/typeof-operator#remarks

下面的测试很好地说明了每个提到的关键字和属性的行为和用法。

Public Sub TestMethod1()

    Dim MyValInt As Integer = 42
    Dim MyValDble As Double = CType(MyValInt, Double)
    Dim MyObj As Object = CType(MyValDble, Object)

    Debug.Print(MyValInt.GetType.ToString) 'Returns System.Int32
    Debug.Print(MyValDble.GetType.ToString) 'Returns System.Double
    Debug.Print(MyObj.GetType.ToString) 'Returns System.Double

    Debug.Print(MyValInt.GetType.GetType.ToString) 'Returns System.RuntimeType
    Debug.Print(MyValDble.GetType.GetType.ToString) 'Returns System.RuntimeType
    Debug.Print(MyObj.GetType.GetType.ToString) 'Returns System.RuntimeType

    Debug.Print(GetType(Integer).GetType.ToString) 'Returns System.RuntimeType
    Debug.Print(GetType(Double).GetType.ToString) 'Returns System.RuntimeType
    Debug.Print(GetType(Object).GetType.ToString) 'Returns System.RuntimeType

    Debug.Print(MyValInt.GetType = GetType(Integer)) '# Returns True
    Debug.Print(MyValInt.GetType = GetType(Double)) 'Returns False
    Debug.Print(MyValInt.GetType = GetType(Object)) 'Returns False

    Debug.Print(MyValDble.GetType = GetType(Integer)) 'Returns False
    Debug.Print(MyValDble.GetType = GetType(Double)) '# Returns True
    Debug.Print(MyValDble.GetType = GetType(Object)) 'Returns False

    Debug.Print(MyObj.GetType = GetType(Integer)) 'Returns False
    Debug.Print(MyObj.GetType = GetType(Double)) '# Returns True
    Debug.Print(MyObj.GetType = GetType(Object)) 'Returns False

    Debug.Print(TypeOf MyObj Is Integer) 'Returns False
    Debug.Print(TypeOf MyObj Is Double) '# Returns True
    Debug.Print(TypeOf MyObj Is Object) '# Returns True


End Sub
Run Code Online (Sandbox Code Playgroud)

编辑

您还可以用来Information.TypeName(Object)获取给定对象的 TypeName。例如,

Dim Foo as Bar
Dim Result as String
Result = TypeName(Foo)
Debug.Print(Result) 'Will display "Bar"
Run Code Online (Sandbox Code Playgroud)