Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Type.IsInstanceOfType Method

Determines whether the specified object is an instance of the current Type.

[Visual Basic]
Public Overridable Function IsInstanceOfType( _
   ByVal o As Object _
) As Boolean
[C#]
public virtual bool IsInstanceOfType(
 object o
);
[C++]
public: virtual bool IsInstanceOfType(
 Object* o
);
[JScript]
public function IsInstanceOfType(
   o : Object
) : Boolean;

Parameters

o
The object to compare with the current Type.

Return Value

true if the current Type is in the inheritance hierarchy of the object represented by the o parameter, or if the current Type is an interface that o supports. false if neither of these conditions is the case, or if o is a null reference (Nothing in Visual Basic).

Remarks

This method can be overridden by a derived class.

Example

[Visual Basic, C#, C++] The following example demonstrates the use of the IsInstanceOfType method.

[Visual Basic] 
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

[C#] 
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));
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
public __gc __interface IMyIfc {};
public __gc class MyClass : public IMyIfc {};
public __gc class MyDerivedClass : public MyClass {};

int main() {
   Type* imyifcType=__typeof(IMyIfc);
   MyClass* mc = new MyClass();
   Type*  mcType = mc->GetType();
   MyClass* mdc = new MyDerivedClass();
   Type*  mdcType = mdc->GetType();
   Int32 array[] = new Int32[10];
   Type*  arrayType = __typeof(Array);
   Console::WriteLine(S"Is Int32[] an instance of the Array class? {0}.", __box(arrayType->IsInstanceOfType(array)));
   Console::WriteLine(S"Is myclass an instance of MyClass? {0}.", __box(mcType->IsInstanceOfType(mc)));
   Console::WriteLine(S"Is myderivedclass an instance of MyClass? {0}.", __box(mcType->IsInstanceOfType(mdc)));
   Console::WriteLine(S"Is myclass an instance of IMyIfc? {0}.", __box(imyifcType->IsInstanceOfType(mc)));
   Console::WriteLine(S"Is myderivedclass an instance of IMyIfc? {0}.", __box(imyifcType->IsInstanceOfType(mdc)));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Type Class | Type Members | System Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.