Type.Equals Metoda

Definice

Určuje, jestli je základní typ systému aktuálního systému stejný jako základní typ systému zadaného Type nebo Object Type .

Přetížení

Equals(Type)

Určuje, zda je typ základního systému aktuálního systému stejný jako základní typ systému Type zadaného Type typu .

Equals(Object)

Určuje, zda je základní typ systému aktuálního objektu stejný jako základní typ systému Type zadaného Object objektu .

Equals(Type)

Určuje, zda je typ základního systému aktuálního systému stejný jako základní typ systému Type zadaného Type typu .

public:
 bool Equals(Type ^ o);
public:
 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

Parametry

o
Type

Objekt, jehož základní typ systému má být porovnán se základním typem systému aktuálního Type objektu .

Návraty

Boolean

true pokud je typ základního o systému stejný jako typ základního systému aktuálního systému , jinak Type false .

Implementuje

Příklady

Následující příklad používá Equals k porovnání dvou typů.


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

Viz také

Platí pro

Equals(Object)

Určuje, zda je základní typ systému aktuálního objektu stejný jako základní typ systému Type zadaného Object objektu .

public:
 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

Parametry

o
Object

Objekt, jehož základní typ systému má být porovnán se základním typem systému aktuálního Type objektu . Aby bylo porovnání úspěšné, musí být možné přetypovat nebo o převést na objekt typu Type .

Návraty

Boolean

true pokud je typ základního o systému stejný jako typ základního systému aktuálního systému , jinak Type false . Tato metoda také false vrátí, pokud:

  • o je null .

  • o nelze přetypovat nebo převést na Type objekt.

Implementuje

Příklady

Následující příklad používá Equals(Object) k porovnání různých instancí objektu s Type Object různými instancemi.

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.

O příkladu stojí zejména za zmínku dvě věci:

  • Porovnání objektu, Type který představuje celé číslo s objektem, který představuje celočíselný TypeInfo návrat, protože true je odvozen z TypeInfo Type .

  • Porovnání objektu, Type který představuje objekt IList<T> (otevřený obecný typ) s objektem List(Of String) (uzavřený obecný typ) vrací false .

Poznámky

Tato metoda přepíše Object.Equals . oPřetypuje na objekt typu a volá Type Type.Equals(Type) metodu .

Viz také

Platí pro