Sdílet prostřednictvím


Type.IsInstanceOfType Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overridable Function IsInstanceOfType ( _
    o As Object _
) As Boolean
public virtual bool IsInstanceOfType(
    Object o
)

Parameters

  • o
    Type: System.Object
    The object to compare with the current Type.

Return Value

Type: System.Boolean
true if the current Type is in the inheritance hierarchy of the object represented by o, or if the current Type is an interface that o supports. false if neither of these conditions is the case, or if o is nulla null reference (Nothing in Visual Basic), or if the current Type is an open generic type (that is, ContainsGenericParameters returns true).

Remarks

This method can be overridden by a derived class.

NoteNote:

A constructed type is not an instance of its generic type definition. That is, MyGenericList<int> (MyGenericList(Of Integer) in Visual Basic) is not an instance of MyGenericList<T> (MyGenericList(Of T) in Visual Basic).

Examples

The following example demonstrates the use of the IsInstanceOfType method.

Public Interface IMyIfc
End Interface 'IMyIfc
Public Class [MyClass]
   Implements IMyIfc
End Class '[MyClass]
Public Class MyDerivedClass
   Inherits [MyClass]
End Class 'MyDerivedClass
Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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)
      outputBlock.Text += String.Format("Is int[] an instance of the Array class? {0}.", arrayType.IsInstanceOfType(array)) & vbCrLf
      outputBlock.Text += String.Format("Is myclass an instance of MyClass? {0}.", mcType.IsInstanceOfType(mc)) & vbCrLf
      outputBlock.Text += String.Format("Is myderivedclass an instance of MyClass? {0}.", mcType.IsInstanceOfType(mdc)) & vbCrLf
      outputBlock.Text += String.Format("Is myclass an instance of IMyIfc? {0}.", imyifcType.IsInstanceOfType(mc)) & vbCrLf
      outputBlock.Text += String.Format("Is myderivedclass an instance of IMyIfc? {0}.", imyifcType.IsInstanceOfType(mdc)) & vbCrLf
   End Sub 'Main
End Class 'IsInstanceTest
using System;
public interface IMyIfc { }
public class MyClass : IMyIfc { }
public class MyDerivedClass : MyClass { }
class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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);
      outputBlock.Text += String.Format("Is int[] an instance of the Array class? {0}.", arrayType.IsInstanceOfType(array)) + "\n";
      outputBlock.Text += String.Format("Is myclass an instance of MyClass? {0}.", mcType.IsInstanceOfType(mc)) + "\n";
      outputBlock.Text += String.Format("Is myderivedclass an instance of MyClass? {0}.", mcType.IsInstanceOfType(mdc)) + "\n";
      outputBlock.Text += String.Format("Is myclass an instance of IMyIfc? {0}.", imyifcType.IsInstanceOfType(mc)) + "\n";
      outputBlock.Text += String.Format("Is myderivedclass an instance of IMyIfc? {0}.", imyifcType.IsInstanceOfType(mdc)) + "\n";
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference