Export (0) Print
Expand All
Expand Minimize

Type.IsAssignableFrom Method

Determines whether an instance of the current Type can be assigned from an instance of the specified Type.

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

public virtual bool IsAssignableFrom (
	Type c
)
public boolean IsAssignableFrom (
	Type c
)
public function IsAssignableFrom (
	c : Type
) : boolean

Parameters

c

The Type to compare with the current Type.

Return Value

true if c and the current Type represent the same type, or if the current Type is in the inheritance hierarchy of c, or if the current Type is an interface that c implements, or if c is a generic type parameter and the current Type represents one of the constraints of c. false if none of these conditions are true, or if c is a null reference (Nothing in Visual Basic).

This method can be overridden by a derived class.

NoteNote

A generic type definition is not assignable from a closed constructed type. That is, you cannot assign the closed constructed type MyGenericList<int> (MyGenericList(Of Integer) in Visual Basic) to a variable of type MyGenericList<T>.

Determine the element types of a Type using GetElementType.

The following example demonstrates the IsAssignableFrom method using arrays.

using System;
class ArrayTypeTest 
{
    public static void Main() 
    {
        int i = 1;
        int [] array10 = new int [10];
        int [] array2 = new int[2];
        int [,]array22 = new int[2,2];
        int [,]array24 = new int[2,4];
        int [,,]array333 = new int[3,3,3];
        Type array10Type = array10.GetType();
        Type array2Type = array2.GetType();
        Type array22Type = array22.GetType();
        Type array24Type = array24.GetType();
        Type array333Type = array333.GetType();

        // If X and Y are not both arrays, then return false.
        Console.WriteLine("Is int[2] assignable from int? {0}.", array2Type.IsAssignableFrom(i.GetType()));
        // If X and Y have same type and rank, then return true.
        Console.WriteLine("Is int[2] assignable from int[10]? {0}.", array2Type.IsAssignableFrom(array10Type));
        Console.WriteLine("Is int[2,2] assignable from int[2,4]? {0}.", array22Type.IsAssignableFrom(array24Type));
        Console.WriteLine("Is int[2,4] assignable from int[2,2]? {0}.", array24Type.IsAssignableFrom(array22Type));
        // If X and Y do not have the same rank, then return false.
        Console.WriteLine("Is int[2,2] assignable from int[10]? {0}.", array22Type.IsAssignableFrom(array10Type));
        Console.WriteLine("Is int[2,2] assignable from int[3,3,3]? {0}.", array22Type.IsAssignableFrom(array333Type));
        Console.WriteLine("Is int[3,3,3] assignable from int[2,2]? {0}.", array333Type.IsAssignableFrom(array22Type));
    }
}

import System.*;

class ArrayTypeTest
{
    public static void main(String[] args)
    {
        int i = 1;
        int array10[] = new int[10];
        int array2[] = new int[2];
        int array22[,] = new int[2, 2];
        int array24[,] = new int[2, 4];
        int array333[, ,] = new int[3, 3, 3];
        Type array10Type = array10.GetType();
        Type array2Type = array2.GetType();
        Type array22Type = array22.GetType();
        Type array24Type = array24.GetType();
        Type array333Type = array333.GetType();

        // If X and Y are not both arrays, then return false.
        Console.WriteLine("Is int[2] assignable from int? {0}.",
            System.Convert.ToString(array2Type.IsAssignableFrom(
            ((Int32)i).GetType())));

        // If X and Y have same type and rank, then return true.
        Console.WriteLine("Is int[2] assignable from int[10]? {0}.", 
            System.Convert.ToString(array2Type.IsAssignableFrom(array10Type)));
        Console.WriteLine("Is int[2,2] assignable from int[2,4]? {0}.",
            System.Convert.ToString(array22Type.IsAssignableFrom(
            array24Type)));
        Console.WriteLine("Is int[2,4] assignable from int[2,2]? {0}.",
            System.Convert.ToString(array24Type.IsAssignableFrom(
            array22Type)));

        // If X and Y do not have the same rank, then return false.
        Console.WriteLine("Is int[2,2] assignable from int[10]? {0}.",
            System.Convert.ToString(array22Type.IsAssignableFrom(
            array10Type)));
        Console.WriteLine("Is int[2,2] assignable from int[3,3,3]? {0}.",
            System.Convert.ToString(array22Type.IsAssignableFrom(
            array333Type)));
        Console.WriteLine("Is int[3,3,3] assignable from int[2,2]? {0}.",
            System.Convert.ToString(array333Type.IsAssignableFrom(
            array22Type)));
    } //main
} //ArrayTypeTest

This code produces the following output:

int[2] is assignable from int? False
     int[2] is assignable from int[10]? True
     int[2,2] is assignable from int[2,4]? True
     int[2,4] is assignable from int[2,2]? True
     int[2,2] is assignable from int[10]? False
     int[2,2] is assignable from int[3,3,3]? False
    int[3,3,3] is assignable from int[2,2]? False

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft