本文整理汇总了VB.NET中System.Type.Equals方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Type.Equals方法的具体用法?VB.NET Type.Equals怎么用?VB.NET Type.Equals使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.Equals方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
' 导入命名空间
Imports System.Reflection
Class Example
Public Shared Sub Main()
Dim a As Type = GetType(System.String)
Dim b As Type = GetType(System.Int32)
Console.WriteLine("{0} = {1}: {2}", a, b, a.Equals(b))
' The Type objects in a and b are not equal,
' because they represent different types.
a = GetType(Example)
b = New Example().GetType()
Console.WriteLine("{0} is equal to {1}: {2}", a, b, a.Equals(b))
' The Type objects in a and b are equal,
' because they both represent type Example.
b = GetType(Type)
Console.WriteLine("typeof({0}).Equals(typeof({1})): {2}", a, b, a.Equals(b))
' The Type objects in a and b are not equal,
' because variable a represents type Example
' and variable b represents type Type.
'Console.ReadLine()
End Sub
End Class
输出:
System.String = System.Int32: False Example is equal to Example: True typeof(Example).Equals(typeof(System.Type)): False
示例2: Example
' 导入命名空间
Imports System.Collections.Generic
Imports System.Reflection
Module Example
Public Sub Main()
Dim t As Type = GetType(Integer)
Dim obj1 As Object = GetType(Integer).GetTypeInfo()
IsEqualTo(t, obj1)
Dim obj2 As Object = GetType(String)
IsEqualTo(t, obj2)
t = GetType(Object)
Dim obj3 As Object = GetType(Object)
IsEqualTo(t, obj3)
t = GetType(List(Of ))
Dim obj4 As Object = (New List(Of String())).GetType()
IsEqualTo(t, obj4)
t = GetType(Type)
Dim obj5 As Object = Nothing
IsEqualTo(t, obj5)
End Sub
Private Sub IsEqualTo(t As Type, inst As Object)
Dim t2 As Type = TryCast(inst, Type)
If t2 IsNot Nothing Then
Console.WriteLine("{0} = {1}: {2}", t.Name, t2.Name,
t.Equals(t2))
Else
Console.WriteLine("Cannot cast the argument to a type.")
End If
Console.WriteLine()
End Sub
End Module
输出:
Int32 = Int32: True Int32 = String: False Object = Object: True List`1 = List`1: False Cannot cast the argument to a type.