Type.Equals Type.Equals Type.Equals Type.Equals Method

Definição

Determina se o tipo de sistema subjacente do Type atual é o mesmo que o tipo de sistema subjacente do Object ou Type especificado.Determines if the underlying system type of the current Type is the same as the underlying system type of the specified Object or Type.

Sobrecargas

Equals(Type) Equals(Type) Equals(Type) Equals(Type)

Determina se o tipo de sistema subjacente do Type atual é o mesmo que o tipo de sistema subjacente do Type especificado.Determines if the underlying system type of the current Type is the same as the underlying system type of the specified Type.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina se o tipo de sistema subjacente do objeto Type atual é o mesmo que o tipo de sistema subjacente do Object especificado.Determines if the underlying system type of the current Type object is the same as the underlying system type of the specified Object.

Equals(Type) Equals(Type) Equals(Type) Equals(Type)

Determina se o tipo de sistema subjacente do Type atual é o mesmo que o tipo de sistema subjacente do Type especificado.Determines if the underlying system type of the current Type is the same as the underlying system type of the specified Type.

public:
 virtual bool Equals(Type ^ o);
public virtual bool Equals (Type o);
override this.Equals : Type -> bool
Public Overridable Function Equals (o As Type) As Boolean

Parâmetros

o
Type Type Type Type

O objeto cujo tipo subjacente do sistema a ser comparado com o tipo de sistema subjacente do atual Type.The object whose underlying system type is to be compared with the underlying system type of the current Type.

Retornos

true se o tipo de sistema subjacente do o for o mesmo que o tipo de sistema subjacente do Type atual; caso contrário, false.true if the underlying system type of o is the same as the underlying system type of the current Type; otherwise, false.

Implementações

Exemplos

O exemplo a seguir usa Equals para comparar dois tipos.The following example uses Equals to compare two types.


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
*/
Imports System
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
'
Veja também

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina se o tipo de sistema subjacente do objeto Type atual é o mesmo que o tipo de sistema subjacente do Object especificado.Determines if the underlying system type of the current Type object is the same as the underlying system type of the specified Object.

public:
 override bool Equals(System::Object ^ o);
public override bool Equals (object o);
override this.Equals : obj -> bool
Public Overrides Function Equals (o As Object) As Boolean

Parâmetros

o
Object Object Object Object

O objeto cujo tipo subjacente do sistema a ser comparado com o tipo de sistema subjacente do atual Type.The object whose underlying system type is to be compared with the underlying system type of the current Type. Para que a comparação seja bem-sucedida, o deve ser capaz de ser convertido em um objeto do tipo Type.For the comparison to succeed, o must be able to be cast or converted to an object of type Type.

Retornos

true se o tipo de sistema subjacente do o for o mesmo que o tipo de sistema subjacente do Type atual; caso contrário, false.true if the underlying system type of o is the same as the underlying system type of the current Type; otherwise, false. Esse método sempre retorna false se: .This method also returns false if: .

  • o é null.
  • o is null.

  • o não pode ser convertido em um objeto Type.
  • o cannot be cast or converted to a Type object.
  • Implementações

    Exemplos

    O exemplo a seguir usa Equals(Object) para comparar vários Type objeto instâncias com vários Object instâncias.The following example uses Equals(Object) to compare various Type object instances with various Object instances.

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

    Duas coisas são particularmente vale a pena observar sobre o exemplo:Two things are particularly worth noting about the example:

    • A comparação de um Type objeto que representa um inteiro com um TypeInfo objeto que representa um inteiro de retorno true porque TypeInfo é derivado de Type.The comparison of a Type object that represents an integer with a TypeInfo object that represents an integer return true because TypeInfo is derived from Type.

    • A comparação de um Type objeto que representa uma IList<T> objeto (um tipo genérico aberto) com um List(Of String) retorna o objeto (um tipo genérico fechado) false.The comparison of a Type object that represents a IList<T> object (an open generic type) with a List(Of String) object (a closed generic type) returns false.

    Comentários

    Este método substitui Object.Equals.This method overrides Object.Equals. Ele projeta o a um objeto do tipo Type e chama o Type.Equals(Type) método.It casts o to an object of type Type and calls the Type.Equals(Type) method.

    Veja também

    Aplica-se a