virtual bool Equals(Type ^ o);
public bool Equals (Type o);
public virtual bool Equals (Type? o);
public virtual bool Equals (Type o);
override this.Equals : Type -> bool
Public Function Equals (o As Type) As Boolean
Public Overridable Function Equals (o As Type) As Boolean
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 = typeof(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 = typeof(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(); /* This code example produces the following output: System.String == System.Int32: False Example is equal to Example: True typeof(Example).Equals(typeof(System.Type)): False open System type Example() = class end let a = typeof<string> let b = typeof<int> printfn $"{a} == {b}: {a.Equals b}" // The Type objects in a and b are not equal, // because they represent different types. let a = typeof<Example> let b = Example().GetType() printfn $"{a} is equal to {b}: {a.Equals b}" // The Type objects in a and b are equal, // because they both represent type Example. let b = typeof<Type> printfn $"typeof({a}).Equals(typeof({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. (* This code example produces the following output: System.String == System.Int32: False Example is equal to Example: True typeof(Example).Equals(typeof(System.Type)): False 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 ' This code example produces the following output: ' System.String = System.Int32: False ' Example is equal to Example: True ' typeof(Example).Equals(typeof(System.Type)): False override bool Equals(System::Object ^ o);
public override bool Equals (object o);
public override bool Equals (object? o);
override this.Equals : obj -> bool
Public Overrides Function Equals (o As Object) As Boolean
Type t2 = inst as Type; if (t2 != null) Console.WriteLine("{0} = {1}: {2}", t.Name, t2.Name, t.Equals(t2)); Console.WriteLine("Cannot cast the argument to a type."); Console.WriteLine(); // The example displays the following output: // Int32 = Int32: True // Int32 = String: False // Object = Object: True // List`1 = List`1: False // Cannot cast the argument to a type. open System open System.Reflection let isEqualTo (t: Type) (inst: obj) = match inst with | :? Type as t2 -> printfn $"{t.Name} = {t2.Name}: {t.Equals t2}\n" | _ -> printfn "Cannot cast the argument to a type.\n" let t = typeof<int> typeof<int>.GetTypeInfo() |> isEqualTo t typeof<String> |> isEqualTo t let t = typeof<obj> typeof<obj> |> isEqualTo t let t = typeof<ResizeArray<_>>.GetGenericTypeDefinition() let obj4: obj = (ResizeArray<String>()).GetType() isEqualTo t obj4 let t = typeof<Type> let obj5: obj = null isEqualTo t obj5 // The example displays the following output: // Int32 = Int32: True // Int32 = String: False // Object = Object: True // List`1 = List`1: False // Cannot cast the argument to a type. 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)) Console.WriteLine("Cannot cast the argument to a type.") End If Console.WriteLine() End Sub End Module ' The example displays the following output: ' Int32 = Int32: True ' Int32 = String: False ' Object = Object: True ' List`1 = List`1: False ' Cannot cast the argument to a type.

這兩件事特別值得注意範例:

  • 物件比較 Type ,該物件代表整數,且 TypeInfo 物件代表整數傳回 true ,因為 TypeInfo 衍生自 Type

  • 物件比較 Type ,表示 IList<T> 物件 (開啟泛型型別) 物件 List(Of String) , (封閉式泛型型別) 傳 false 回 。

    即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱: https://aka.ms/ContentUserFeedback

    提交並檢視相關的意見反應

  •