Type.Equals 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
오버로드
| Equals(Type) | |
| Equals(Object) |
Equals(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
매개 변수
반환
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
*/
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)
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을(를) 반환합니다.
o이null인 경우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.
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 TypeInfo
true가에서 파생 되므로 정수 반환을 나타내는 개체를 사용 하 여 정수를 나타내는 개체의 비교입니다 TypeInfo Type .개체 ( Type 개방형 제네릭 형식)를 나타내는 개체를 개체 IList<T> (폐쇄형 제네릭 형식)와 비교 하면이
List(Of String)반환false됩니다.
설명
이 메서드는 Object.Equals를 재정의합니다. o형식의 개체로 캐스팅 하 Type 고 메서드를 호출 Type.Equals(Type) 합니다.