Type.IsAssignableFrom Method (Type)
Determines whether an instance of a specified type can be assigned to an instance of the current type.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- c
-
Type:
System.Type
The type to compare with the current type.
Return Value
Type: System.Booleantrue 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. c is derived directly from the current instance if it inherits from the current instance; c is derived indirectly from the current instance if it inherits from a succession of one or more classes that inherit 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.
In the following example, the current instance is a Type object that represents the Stream class. GenericWithConstraint is a generic type whose generic type parameter must be of type Stream. Passing its generic type parameter to the IsAssignableFrom indicates that an instance of the generic type parameter can be assigned to an Stream object.
Imports System.IO Module Example Public Sub Main() Dim t As Type = GetType(Stream) Dim genericT As Type = GetType(GenericWithConstraint(Of )) Dim genericParam As Type = genericT.GetGenericArguments()(0) Console.WriteLine(t.IsAssignableFrom(genericParam)) ' Displays True. End Sub End Module Public Class GenericWithConstraint(Of T As Stream) End Class
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 null.
Implements
_Type.IsAssignableFrom(Type)The IsAssignableFrom method can be used to determine whether an instance of c can be assigned to an instance of the current type, The method is most useful when you are handling objects whose types are not known at design time and allows for conditional assignment, as the following example shows.
Imports System.Collections Module Example Public Sub Main() Dim t As Type = GetType(IEnumerable) Dim c As Type = GetType(Array) Dim instanceOfT As IEnumerable Dim instanceOfC As Integer() = { 1, 2, 3, 4 } If t.IsAssignableFrom(c) Then instanceOfT = instanceOfC End If End Sub End Module
This method thus ensures that a line of code like the following will execute at runtime without throwing an InvalidCastException exception or a similar exception:
This method can be overridden by a derived class.
Note |
|---|
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
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
