This documentation is archived and is not being maintained.

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)

'Declaration
Public Overridable Function IsAssignableFrom ( _
	c As Type _
) As Boolean
'Usage
Dim instance As Type 
Dim c As Type 
Dim returnValue As Boolean 

returnValue = instance.IsAssignableFrom(c)

Parameters

c
Type: System.Type

The Type to compare with the current Type.

Return Value

Type: System.Boolean
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 Nothing.

Implements

_Type.IsAssignableFrom(Type)

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>.

You can determine the element types of a Type using GetElementType.

If the c parameter is of type TypeBuilder, the result is based on the type that is to be built. The following code example demonstrates this using a built type named B.

TypeBuilder b1 = moduleBuilder.DefineType("B", TypeAttributes.Public, typeof(A));
// Returns true:
typeof(A).IsAssignableFrom(b1))

The following example demonstrates the IsAssignableFrom method using defined classes, integer arrays, and generics.

Imports System
Imports System.Collections.Generic

Class ArrayTypeTest

    Public Shared Sub Main()
        ' Demonstrate classes:
        Console.WriteLine("Defned Classes:")
        Dim room1 As Room = New Room
        Dim kitchen1 As Kitchen = New Kitchen
        Dim bedroom1 As Bedroom = New Bedroom
        Dim guestroom1 As Guestroom = New Guestroom
        Dim masterbedroom1 As MasterBedroom = New MasterBedroom

        Dim room1Type As Type = room1.GetType
        Dim kitchen1Type As Type = kitchen1.GetType
        Dim bedroom1Type As Type = bedroom1.GetType
        Dim guestroom1Type As Type = guestroom1.GetType
        Dim masterbedroom1Type As Type = masterbedroom1.GetType

        Console.WriteLine("room assignable from kitchen: {0}", room1Type.IsAssignableFrom(kitchen1Type))
        Console.WriteLine("bedroom assignable from guestroom: {0}", bedroom1Type.IsAssignableFrom(guestroom1Type))
        Console.WriteLine("kitchen assignable from masterbedroom: {0}", kitchen1Type.IsAssignableFrom(masterbedroom1Type))

        ' Demonstrate arrays:
        Console.WriteLine()
        Console.WriteLine("Integer arrays:")

        Dim array10(10) As Integer 
        Dim array2(2) As Integer 
        Dim array22(2, 2) As Integer 
        Dim array24(2, 4) As Integer 

        Dim array10Type As Type = array10.GetType
        Dim array2Type As Type = array2.GetType
        Dim array22Type As Type = array22.GetType
        Dim array24Type As Type = array24.GetType

        Console.WriteLine("Integer(2) assignable from Integer(10): {0}", array2Type.IsAssignableFrom(array10Type))
        Console.WriteLine("Integer(2) assignable from Integer(2,4): {0}", array2Type.IsAssignableFrom(array24Type))
        Console.WriteLine("Integer(2,4) assignable from Integer(2,2): {0}", array24Type.IsAssignableFrom(array22Type))

        ' Demonstrate generics:
        Console.WriteLine()
        Console.WriteLine("Generics:")

        Dim arrayNull(10) As Nullable(Of Integer)
        Dim genIntList As New List(Of Integer)
        Dim genTList As New List(Of Type)

        Dim arrayNullType As Type = arrayNull.GetType
        Dim genIntListType As Type = genIntList.GetType
        Dim genTListType As Type = genTList.GetType

        Console.WriteLine("Integer(10) assignable from Nullable(Of Integer)(10): {0}", array10Type.IsAssignableFrom(arrayNullType))
        Console.WriteLine("List(Of Integer) assignable from List(Of Type): {0}", genIntListType.IsAssignableFrom(genTListType))
        Console.WriteLine("List(Of Type) assignable from List(Of Integer): {0}", genTListType.IsAssignableFrom(genIntListType))
        Console.ReadLine()
    End Sub 
End Class 
Class Room
End Class 
Class Kitchen
    Inherits Room
End Class 
Class Bedroom
    Inherits Room
End Class 
Class Guestroom
    Inherits Bedroom
End Class 
Class MasterBedroom
    Inherits Bedroom
End Class 

'This code example produces the following output: 

' Defned Classes: 
' room assignable from kitchen: True 
' bedroom assignable from guestroom: True 
' kitchen assignable from masterbedroom: False 

' Integer arrays: 
' Integer(2) assignable from Integer(10): True 
' Integer(2) assignable from Integer(2,4): False 
' Integer(2,4) assignable from Integer(2,2): True 

' Generics: 
' Integer(10) assignable from Nullable(Of Integer)(10): False 
' List(Of Integer) assignable from List(Of Type): False 
' List(Of Type) assignable from List(Of Integer): False

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show: