本文整理匯總了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.