Type.IsAssignableFrom Method
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)
public boolean IsAssignableFrom ( Type c )
public function IsAssignableFrom ( c : Type ) : boolean
Not applicable.
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.
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>. |
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("int[2] assignable from int[10]: {0}", array2Type.IsAssignableFrom(array10Type)) Console.WriteLine("int[2] assignable from int[2,4]: {0}", array2Type.IsAssignableFrom(array24Type)) Console.WriteLine("int[2,4] assignable from int[2,2]: {0}", array24Type.IsAssignableFrom(array22Type)) ' Demonstrate generics: Console.WriteLine() Console.WriteLine("Generics:") ' Note that "int?[]" is the same as "Nullable<int>[]" 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("int[10] assignable from int?[10]: {0}", array10Type.IsAssignableFrom(arrayNullType)) Console.WriteLine("List<int> assignable from List<Type>: {0}", genIntListType.IsAssignableFrom(array10Type)) Console.WriteLine("List<Type> assignable from List<int>: {0}", genTListType.IsAssignableFrom(genTListType)) 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: int[2] assignable from int[10]: True int[2] assignable from int[2,4]: False int[2,4] assignable from int[2,2]: True Generics: int[10] assignable from int?[10]: False List<int> assignable from List<Type>: False List<Type> assignable from List<int>: True
Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: