閱讀英文版本

分享方式:


Type.Equals 方法

定義

判斷目前 Type 的基礎系統類型,是否與指定的 ObjectType 的基礎系統類型相同。

多載

Equals(Type)

判斷目前 Type 的基礎系統類型,是否與指定的 Type 之基礎系統類型相同。

Equals(Object)

判斷目前 Type 物件的基礎系統類型,是否與指定的 Object 的基礎系統類型相同。

Equals(Type)

來源:
Type.cs
來源:
Type.cs
來源:
Type.cs

判斷目前 Type 的基礎系統類型,是否與指定的 Type 之基礎系統類型相同。

C#
public bool Equals (Type o);
C#
public virtual bool Equals (Type? o);
C#
public virtual bool Equals (Type o);

參數

o
Type

其基礎系統類型將與目前 Type 的基礎系統類型比較的物件。

傳回

如果 true 的基礎系統類型與目前 o 的基礎系統類型相同,則為 Type否則為 false

實作

範例

下列範例會使用 Equals 來比較兩種類型。

C#

using System;
using System.Reflection;

class Example
{
    public static void Main()
    {

        Type a = typeof(System.String);
        Type b = typeof(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 = 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
*/

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Equals(Object)

來源:
Type.cs
來源:
Type.cs
來源:
Type.cs

判斷目前 Type 物件的基礎系統類型,是否與指定的 Object 的基礎系統類型相同。

C#
public override bool Equals (object o);
C#
public override bool Equals (object? o);

參數

o
Object

其基礎系統類型將與目前 Type 的基礎系統類型比較的物件。 若要讓比較成功, o 必須能夠轉換或轉換成 類型 Type 的物件。

傳回

如果 true 的基礎系統類型與目前 o 的基礎系統類型相同,則為 Type否則為 false。 如果是下列情況,這個方法也會傳回 false

  • onull

  • o 無法轉型或轉換為 Type 物件。

實作

範例

下列範例會使用 Equals(Object) 來比較各種 Type 物件實例與各種 Object 實例。

C#
using System;
using System.Collections.Generic;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      Type t =typeof(int);
      Object obj1 = typeof(int).GetTypeInfo();
      IsEqualTo(t, obj1);

      Object obj2 = typeof(String);
      IsEqualTo(t, obj2);
      
      t = typeof(Object);
      Object obj3 = typeof(Object);
      IsEqualTo(t, obj3);
      
      t = typeof(List<>);
      Object obj4 = (new List<String>()).GetType();
      IsEqualTo(t, obj4);
      
      t = typeof(Type);
      Object obj5 = null;
      IsEqualTo(t, obj5);
   }
   
   private static void IsEqualTo(Type t, Object inst)
   {
      Type t2 = inst as Type;
      if (t2 != null)
         Console.WriteLine("{0} = {1}: {2}", t.Name, t2.Name,
                           t.Equals(t2));
      else
         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.

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

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

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

備註

這個方法會覆寫 Object.Equals。 它會轉換成 oType 別的物件,並呼叫 Type.Equals(Type) 方法。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0