Type.IsInstanceOfType(Object) Метод
Определение
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Определяет, является ли указанный объект экземпляром текущего Typeобъекта.
public:
virtual bool IsInstanceOfType(System::Object ^ o);
public virtual bool IsInstanceOfType(object? o);
public virtual bool IsInstanceOfType(object o);
abstract member IsInstanceOfType : obj -> bool
override this.IsInstanceOfType : obj -> bool
Public Overridable Function IsInstanceOfType (o As Object) As Boolean
Параметры
- o
- Object
Объект для сравнения с текущим типом.
Возвращаемое значение
trueЗначение , если текущий Type находится в иерархии наследования объекта, представленного oили текущим является интерфейсом, Type реализующим.o
falseЕсли ни в чем из этих условий нет, если это oтак, null или если текущий Type является открытым универсальным типом (то есть ContainsGenericParameters возвращает).true
Реализации
Примеры
В следующем примере иллюстрируется использование метода IsInstanceOfType.
using System;
public interface IExample {}
public class BaseClass : IExample {}
public class DerivedClass : BaseClass {}
public class Example
{
public static void Main()
{
var interfaceType = typeof(IExample);
var base1 = new BaseClass();
var base1Type = base1.GetType();
var derived1 = new DerivedClass();
var derived1Type = derived1.GetType();
int[] arr = new int[11];
Console.WriteLine("Is int[] an instance of the Array class? {0}.",
typeof(Array).IsInstanceOfType(arr));
Console.WriteLine("Is base1 an instance of BaseClass? {0}.",
base1Type.IsInstanceOfType(base1));
Console.WriteLine("Is derived1 an instance of BaseClass? {0}.",
base1Type.IsInstanceOfType(derived1));
Console.WriteLine("Is base1 an instance of IExample? {0}.",
interfaceType.IsInstanceOfType(base1));
Console.WriteLine("Is derived1 an instance of IExample? {0}.",
interfaceType.IsInstanceOfType(derived1));
}
}
// The example displays the following output:
// Is int[] an instance of the Array class? True.
// Is base1 an instance of BaseClass? True.
// Is derived1 an instance of BaseClass? True.
// Is base1 an instance of IExample? True.
// Is derived1 an instance of IExample? True.
open System
type IExample = interface end
type BaseClass() = interface IExample
type DerivedClass() = inherit BaseClass()
let interfaceType = typeof<IExample>
let base1 = BaseClass()
let base1Type = base1.GetType()
let derived1 = DerivedClass()
let derived1Type = derived1.GetType()
let arr = Array.zeroCreate<int> 11
printfn $"Is int[] an instance of the Array class? {typeof<Array>.IsInstanceOfType arr}."
printfn $"Is base1 an instance of BaseClass? {base1Type.IsInstanceOfType base1}."
printfn $"Is derived1 an instance of BaseClass? {base1Type.IsInstanceOfType derived1}."
printfn $"Is base1 an instance of IExample? {interfaceType.IsInstanceOfType base1}."
printfn $"Is derived1 an instance of IExample? {interfaceType.IsInstanceOfType derived1}."
// The example displays the following output:
// Is int[] an instance of the Array class? True.
// Is base1 an instance of BaseClass? True.
// Is derived1 an instance of BaseClass? True.
// Is base1 an instance of IExample? True.
// Is derived1 an instance of IExample? True.
Public Interface IExample
End Interface
Public Class BaseClass : Implements IExample
End Class
Public Class DerivedClass : Inherits BaseClass
End Class
Public Module Example
Public Sub Main()
Dim interfaceType As Type = GetType(IExample)
Dim base1 As New BaseClass()
Dim base1Type As Type = base1.GetType()
Dim derived1 = New DerivedClass()
Dim derived1Type As Type = derived1.GetType()
Dim arr(10) As Integer
Console.WriteLine("Is int[] an instance of the Array class? {0}.",
GetType(Array).IsInstanceOfType(arr))
Console.WriteLine("Is base1 an instance of BaseClass? {0}.",
base1Type.IsInstanceOfType(base1))
Console.WriteLine("Is derived1 an instance of BaseClass? {0}.",
base1Type.IsInstanceOfType(derived1))
Console.WriteLine("Is base1 an instance of IExample? {0}.",
interfaceType.IsInstanceOfType(base1))
Console.WriteLine("Is derived1 an instance of IExample? {0}.",
interfaceType.IsInstanceOfType(derived1))
End Sub
End Module
' The example displays the following output:
' Is int[] an instance of the Array class? True.
' Is base1 an instance of BaseClass? True.
' Is derived1 an instance of BaseClass? True.
' Is base1 an instance of IExample? True.
' Is derived1 an instance of IExample? True.
Комментарии
Этот метод можно переопределить производным классом.
Note
Созданный тип не является экземпляром его определения универсального типа. То есть MyGenericList<int> (MyGenericList(Of Integer) в Visual Basic) не является экземпляром MyGenericList<T> (MyGenericList(Of T) в Visual Basic).