Type.IsInstanceOfType 方法
确定指定的对象是否是当前 Type 的实例。
**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Overridable Function IsInstanceOfType ( _
o As Object _
) As Boolean
用法
Dim instance As Type
Dim o As Object
Dim returnValue As Boolean
returnValue = instance.IsInstanceOfType(o)
public virtual bool IsInstanceOfType (
Object o
)
public:
virtual bool IsInstanceOfType (
Object^ o
)
public boolean IsInstanceOfType (
Object o
)
public function IsInstanceOfType (
o : Object
) : boolean
参数
- o
将与当前 Type 进行比较的对象。
返回值
如果当前 Type 在 o 表示的对象的继承层次结构中,或者如果当前 Type 是 o 支持的接口,则为 true。如果不属于这其中任一种情况,或者如果 o 为 空引用(在 Visual Basic 中为 Nothing),或者如果当前 Type 为开放泛型类型(即 ContainsGenericParameters 返回 true),则为 false。
备注
此方法可由派生类重写。
提示
构造类型不是其泛型类型定义的实例。也就是说,MyGenericList<int>
(在 Visual Basic 中为 MyGenericList(Of Integer)
)不是 MyGenericList<T>
(在 Visual Basic 中为 MyGenericList(Of T)
)的实例。
示例
下面的示例演示 IsInstanceOfType 方法的用法。
Imports System
Public Interface IMyIfc
End Interface 'IMyIfc
Public Class [MyClass]
Implements IMyIfc
End Class '[MyClass]
Public Class MyDerivedClass
Inherits [MyClass]
End Class 'MyDerivedClass
Class IsInstanceTest
Public Shared Sub Main()
Dim imyifcType As Type = GetType(IMyIfc)
Dim mc As New [MyClass]()
Dim mcType As Type = mc.GetType()
Dim mdc = New MyDerivedClass()
Dim mdcType As Type = mdc.GetType()
Dim array(10) As Integer
Dim arrayType As Type = GetType(Array)
Console.WriteLine("Is int[] an instance of the Array class? {0}.", arrayType.IsInstanceOfType(array))
Console.WriteLine("Is myclass an instance of MyClass? {0}.", mcType.IsInstanceOfType(mc))
Console.WriteLine("Is myderivedclass an instance of MyClass? {0}.", mcType.IsInstanceOfType(mdc))
Console.WriteLine("Is myclass an instance of IMyIfc? {0}.", imyifcType.IsInstanceOfType(mc))
Console.WriteLine("Is myderivedclass an instance of IMyIfc? {0}.", imyifcType.IsInstanceOfType(mdc))
End Sub 'Main
End Class 'IsInstanceTest
using System;
public interface IMyIfc {}
public class MyClass : IMyIfc {}
public class MyDerivedClass : MyClass {}
class IsInstanceTest
{
public static void Main()
{
Type imyifcType=typeof(IMyIfc);
MyClass mc = new MyClass();
Type mcType = mc.GetType();
MyClass mdc = new MyDerivedClass();
Type mdcType = mdc.GetType();
int [] array = new int [10];
Type arrayType = typeof(Array);
Console.WriteLine("Is int[] an instance of the Array class? {0}.", arrayType.IsInstanceOfType(array));
Console.WriteLine("Is myclass an instance of MyClass? {0}.", mcType.IsInstanceOfType(mc));
Console.WriteLine("Is myderivedclass an instance of MyClass? {0}.", mcType.IsInstanceOfType(mdc));
Console.WriteLine("Is myclass an instance of IMyIfc? {0}.", imyifcType.IsInstanceOfType(mc));
Console.WriteLine("Is myderivedclass an instance of IMyIfc? {0}.", imyifcType.IsInstanceOfType(mdc));
}
}
using namespace System;
interface class IMyIfc{};
public ref class MyClass: public IMyIfc{};
public ref class MyDerivedClass: public MyClass{};
int main()
{
Type^ imyifcType = IMyIfc::typeid;
MyClass^ mc = gcnew MyClass;
Type^ mcType = mc->GetType();
MyClass^ mdc = gcnew MyDerivedClass;
Type^ mdcType = mdc->GetType();
array<Int32>^arr = gcnew array<Int32>(10);
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 MyClass? {0}.", mcType->IsInstanceOfType( mc ) );
Console::WriteLine( "Is myderivedclass an instance of MyClass? {0}.", mcType->IsInstanceOfType( mdc ) );
Console::WriteLine( "Is myclass an instance of IMyIfc? {0}.", imyifcType->IsInstanceOfType( mc ) );
Console::WriteLine( "Is myderivedclass an instance of IMyIfc? {0}.", imyifcType->IsInstanceOfType( mdc ) );
}
import System.*;
public interface IMyIfc
{
} //IMyIfc
public class MyClass implements IMyIfc
{
} //MyClass
public class MyDerivedClass extends MyClass
{
} //MyDerivedClass
class IsInstanceTest
{
public static void main(String[] args)
{
Type imyifcType = IMyIfc.class.ToType();
MyClass mc = new MyClass();
Type mcType = mc.GetType();
MyClass mdc = new MyDerivedClass();
Type mdcType = mdc.GetType();
int array[] = new int[10];
Type arrayType = Array.class.ToType();
Console.WriteLine("Is int[] an instance of the Array class? {0}.",
System.Convert.ToString(arrayType.IsInstanceOfType(array)));
Console.WriteLine("Is myclass an instance of MyClass? {0}.",
System.Convert.ToString(mcType.IsInstanceOfType(mc)));
Console.WriteLine("Is myderivedclass an instance of MyClass? {0}.",
System.Convert.ToString(mcType.IsInstanceOfType(mdc)));
Console.WriteLine("Is myclass an instance of IMyIfc? {0}.",
System.Convert.ToString(imyifcType.IsInstanceOfType(mc)));
Console.WriteLine("Is myderivedclass an instance of IMyIfc? {0}.",
System.Convert.ToString(imyifcType.IsInstanceOfType(mdc)));
} //main
} //IsInstanceTest
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0