System.Type.Equals 方法 (Object)

方法描述

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

语法定义(C# System.Type.Equals 方法 (Object) 的用法)

public override bool Equals(
	Object o
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
o System-Object 该对象,其基础系统类型将与当前 Type 的基础系统类型相比较。
返回值 System.Boolean 如果 o 的基础系统类型与当前 Type 的基础系统类型相同,则为 true;否则为 false。 如果 o 参数指定的对象不是 Type,此方法也会返回 false。

提示和注释

此方法重写 Object.Equals。

System.Type.Equals 方法 (Object)例子

下面的示例使用 Equals 比较两个 Object 实例。

using System;
using System.Reflection;

class Example
{
    public static void Main()
    {

        Type a = typeof(System.Object);
        Type b = typeof(System.Object);

        Console.WriteLine("{0} == {1}: {2}", a, b, a.Equals(b));

        // The Type objects in a and b are equal,
        // because they represent System.Object.

        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(Object);

        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 Object.

        //Console.ReadLine();

    }
}

//
/* This code example produces the following output:
    System.Object == System.Object: True
    Example is equal to Example: True
    typeof(Example).Equals(typeof(System.Type)): False
*/

异常

异常 异常描述

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。