Type.IsInstanceOfType(Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
确定指定的对象是否是当前 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
实现的接口。 如果不属于其中任一种情况,o
为 null
,或者当前 Type
为开放式泛型类型(即 ContainsGenericParameters 返回 true
),则为 false
。
实现
示例
下面的示例演示 IsInstanceOfType
方法的用法。
using namespace System;
public interface class IExample{};
public ref class BaseClass: IExample{};
public ref class DerivedClass: BaseClass{};
void main()
{
Type^ interfaceType = IExample::typeid;
BaseClass^ base1 = gcnew BaseClass;
Type^ base1Type = base1->GetType();
BaseClass^ derived1 = gcnew DerivedClass;
Type^ derived1Type = derived1->GetType();
array<Int32>^ arr = gcnew array<Int32>(11);
Type^ arrayType = Array::typeid;
Console::WriteLine("Is Int32[] an instance of the Array class? {0}.",
arrayType->IsInstanceOfType( arr ) );
Console::WriteLine("Is myclass an instance of BaseClass? {0}.",
base1Type->IsInstanceOfType( base1 ) );
Console::WriteLine("Is myderivedclass an instance of BaseClass? {0}.",
base1Type->IsInstanceOfType( derived1 ) );
Console::WriteLine("Is myclass an instance of IExample? {0}.",
interfaceType->IsInstanceOfType( base1 ) );
Console::WriteLine("Is myderivedclass 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.
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.
注解
派生类可以重写此方法。
注意
构造类型不是其泛型类型定义的实例。 也就是说, MyGenericList<int>
Visual Basic) MyGenericList(Of Integer)
中的 (不是 Visual Basic) 中 (MyGenericList(Of T)
的实例MyGenericList<T>
。