Type.Equals 方法

定义

确定当前 Type 的基础系统类型是否与指定 ObjectType 的基础系统类型相同。

重载

Equals(Type)

确定当前 Type 的基础系统类型是否与指定 Type 的基础系统类型相同。

Equals(Object)

确定当前 Type 的基础系统类型是否与指定 Object 的基础系统类型相同。

Equals(Type)

Source:
Type.cs
Source:
Type.cs
Source:
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)

Source:
Type.cs
Source:
Type.cs
Source:
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) 将各种对象实例与各种TypeObject实例进行比较。

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

  • 表示打开泛型类型) 对象 (对象与封闭泛型类型 (对象的比较TypeIList<T>) 返回 falseList(Of String)

注解

此方法重写 Object.Equals。 它强制转换为 o 类型的 Type 对象并调用 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