Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

Type.IsAssignableFrom Method

Determines whether an instance of a specified type can be assigned to the current type instance.

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

'Declaration
Public Overridable Function IsAssignableFrom ( _
	c As Type _
) As Boolean

Parameters

c
Type: System.Type

The type to compare with the current type.

Return Value

Type: System.Boolean
true if any of the following conditions is true:
  • c and the current instance represent the same type.

  • c is derived either directly or indirectly from the current instance.

  • The current instance is an interface that c implements.

  • c is a generic type parameter, and the current instance represents one of the constraints of c.

  • c represents a value type, and the current instance represents Nullable<c> (Nullable(Of c) in Visual Basic).

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

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.

Imports System.Reflection
Imports System.Reflection.Emit

Public Class A
End Class 

Module Example
   Public Sub Main()
      Dim domain As AppDomain = AppDomain.CurrentDomain
      Dim assemName As New AssemblyName()
      assemName.Name = "TempAssembly" 

      ' Define a dynamic assembly in the current application domain. 
      Dim assemBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemName,
                                                   AssemblyBuilderAccess.Run)

      ' Define a dynamic module in this assembly. 
      Dim moduleBuilder As ModuleBuilder = assemBuilder.DefineDynamicModule("TempModule")

      Dim b1 As TypeBuilder = moduleBuilder.DefineType("B", TypeAttributes.Public, GetType(A))
      Console.WriteLine(GetType(A).IsAssignableFrom(b1))
   End Sub 
End Module 
' The example displays the following output: 
'       True

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

Imports System.Collections.Generic

Module Example
    Public Sub Main()
        Console.WriteLine("Defined Classes:")
        Dim room1 As New Room()
        Dim kitchen1 As New Kitchen()
        Dim bedroom1 As New Bedroom()
        Dim guestroom1 As New Guestroom()
        Dim masterbedroom1 As 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 Module 

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 
' The example displays the following output: 
'    Defined 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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft