Mono Class Library: System.Type Overview | Members

System.Type.IsInstanceOfType Method

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

public virtual bool IsInstanceOfType (object o)

Parameters

o
The object to compare with the current Type.

Returns

true if either of the following statements is true; otherwise false.

  • If the current Type is in the inheritance hierarchy of o.
  • If the current Type is an interface and o supports that interface.

If o is a null reference or if the current instance is an open generic type (that is, Type.ContainsGenericParameters returns true) returns false.

Remarks

As described above.

Note:

A constructed type is not an instance of its generic type definition.

Operation
As described above.

Example

The following example demonstrates the Type.IsInstanceOfType(object) method.

C# Example

using System;
public interface IFoo { }
public class MyClass : IFoo {}
public class MyDerivedClass : MyClass {}
class IsInstanceTest {
 public static void Main() {
 Type ifooType=typeof(IFoo);
 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("int[] is instance of Array? {0}", arrayType.IsInstanceOfType(array));
 Console.WriteLine("myclass instance is instance of MyClass? {0}", mcType.IsInstanceOfType(mc));
 Console.WriteLine("myderivedclass instance is instance of MyClass? {0}",  mcType.IsInstanceOfType(mdc));
 Console.WriteLine("myclass instance is instance of IFoo? {0}", ifooType.IsInstanceOfType(mc));
 Console.WriteLine("myderivedclass instance is instance of IFoo? {0}",  ifooType.IsInstanceOfType(mdc));
 }
}

The output is

int[] is instance of Array? True
myclass instance is instance of MyClass? True
myderivedclass instance is instance of MyClass? True
myclass instance is instance of IFoo? True
myderivedclass instance is instance of IFoo? True

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0