Type.Equals 메서드

정의

현재 Type 기본 시스템 형식이 지정된 Object 시스템 형식의 기본 시스템 형식과 같은지 또는 Type.

오버로드

Name Description
Equals(Type)

현재 Type 기본 시스템 형식이 지정된 Type기본 시스템 형식과 같은지 확인합니다.

Equals(Object)

현재 Type 개체의 기본 시스템 형식이 지정된 Object기본 시스템 형식과 같은지 확인합니다.

Equals(Type)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

현재 Type 기본 시스템 형식이 지정된 Type기본 시스템 형식과 같은지 확인합니다.

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

매개 변수

o
Type

기본 시스템 형식을 현재 Type시스템의 기본 시스템 형식과 비교할 개체입니다.

반품

true 기본 시스템 형식 o 이 현재 Type시스템의 기본 시스템 형식과 같으면 이고, false그렇지 않으면 .

구현

예제

다음 예제에서는 두 형식을 비교하는 데 사용합니다 Equals .


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
*/
open System

type Example() = class end

do
    let a = typeof<string>
    let b = typeof<int>

    printfn $"{a} == {b}: {a.Equals b}"

    // The Type objects in a and b are not equal,
    // because they represent different types.

    let a = typeof<Example>
    let b = Example().GetType()

    printfn $"{a} is equal to {b}: {a.Equals b}"

    // The Type objects in a and b are equal,
    // because they both represent type Example.

    let b = typeof<Type>

    printfn $"typeof({a}).Equals(typeof({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.

(* 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
'

추가 정보

적용 대상

Equals(Object)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

현재 Type 개체의 기본 시스템 형식이 지정된 Object기본 시스템 형식과 같은지 확인합니다.

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

매개 변수

o
Object

기본 시스템 형식을 현재 Type시스템의 기본 시스템 형식과 비교할 개체입니다. 비교가 성공 o 하려면 형식 Type의 개체로 캐스팅하거나 변환할 수 있어야 합니다.

반품

true 기본 시스템 형식 o 이 현재 Type시스템의 기본 시스템 형식과 같으면 이고, false그렇지 않으면 . 이 메서드는 다음 경우에도 반환합니다.false

  • onull입니다.

  • o 은 개체로 캐스팅하거나 변환할 Type 수 없습니다.

구현

예제

다음 예제에서는 다양한 개체 인스턴스를 다양한 Equals(Object)Type 인스턴스와 비교하는 데 사용합니다Object.

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.
open System
open System.Reflection

let isEqualTo (t: Type) (inst: obj) =
    match inst with
    | :? Type as t2 ->
        printfn $"{t.Name} = {t2.Name}: {t.Equals t2}\n"
    | _ ->
        printfn "Cannot cast the argument to a type.\n"

do 
    let t = typeof<int>
    typeof<int>.GetTypeInfo()
    |> isEqualTo t

    typeof<String>
    |> isEqualTo t

    let t = typeof<obj>
    typeof<obj>
    |> isEqualTo t

    let t = typeof<ResizeArray<_>>.GetGenericTypeDefinition()
    let obj4: obj = (ResizeArray<String>()).GetType()
    isEqualTo t obj4

    let t = typeof<Type>
    let obj5: obj = null
    isEqualTo t obj5
// 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.

이 예제에는 특히 주목할 만한 두 가지 사항이 있습니다.

  • 에서 파생Type되므로 정수 반환 TypeInfotrue 을 나타내는 개체와 TypeInfo 정수 값을 나타내는 개체의 비교 Type 입니다.

  • 개체(닫힌 제네릭 형식)와 Type 개체(열린 제네릭 형식)를 나타내는 IList<T> 개체의 비교 List(Of String) 가 반환됩니다false.

설명

이 메서드는 Object.Equals를 재정의합니다. 형식 o 의 개체로 캐스팅 Type 되고 메서드를 호출합니다Type.Equals(Type).

추가 정보

적용 대상