Type.IsAssignableFrom Method
Microsoft Silverlight will reach end of support after October 2021. Learn more.
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)
Syntax
'Declaration
Public Overridable Function IsAssignableFrom ( _
c As Type _
) As Boolean
public virtual bool IsAssignableFrom(
Type 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 nulla null reference (Nothing in Visual Basic).
Remarks
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))
Examples
The following example demonstrates the IsAssignableFrom method using defined classes, integer arrays, and generics.
Note: |
---|
To run this example, see Building Examples That Use a Demo Method and a TextBlock Control. |
Imports System.Collections.Generic
Class Example
Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
' Demonstrate classes:
outputBlock.Text &= "Defined Classes:" & vbCrLf
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
outputBlock.Text &= String.Format("room assignable from kitchen: {0}", _
room1Type.IsAssignableFrom(kitchen1Type)) & vbCrLf
outputBlock.Text &= String.Format("bedroom assignable from guestroom: {0}", _
bedroom1Type.IsAssignableFrom(guestroom1Type)) & vbCrLf
outputBlock.Text &= String.Format("kitchen assignable from masterbedroom: {0}", _
kitchen1Type.IsAssignableFrom(masterbedroom1Type)) & vbCrLf
' Demonstrate arrays:
outputBlock.Text &= vbCrLf
outputBlock.Text &= "Integer arrays:" & vbCrLf
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
outputBlock.Text &= String.Format("Integer(2) assignable from Integer(10): {0}", _
array2Type.IsAssignableFrom(array10Type)) & vbCrLf
outputBlock.Text &= String.Format("Integer(2) assignable from Integer(2,4): {0}", _
array2Type.IsAssignableFrom(array24Type)) & vbCrLf
outputBlock.Text &= String.Format("Integer(2,4) assignable from Integer(2,2): {0}", _
array24Type.IsAssignableFrom(array22Type)) & vbCrLf
' Demonstrate generics:
outputBlock.Text &= vbCrLf
outputBlock.Text &= "Generics:" & vbCrLf
' Note that "int?[]" is the same as "Nullable<int>[]"
Dim arrayNull(10) As Nullable(Of Integer)
Dim genIntList As New List(Of Integer)
Dim genObjList As New List(Of Object)
Dim genExList As New List(Of Exception)
Dim arrayNullType As Type = arrayNull.GetType
Dim genIntListType As Type = genIntList.GetType
Dim genObjListType As Type = genObjList.GetType
Dim genExListType As Type = genExList.GetType
outputBlock.Text &= _
String.Format("Integer(10) assignable from Nullable(Of Integer)(10): {0}", _
array10Type.IsAssignableFrom(arrayNullType)) & vbCrLf
outputBlock.Text &= _
String.Format("List(Of Object) assignable from List(Of Exception): {0}", _
genObjListType.IsAssignableFrom(genExListType)) & vbCrLf
outputBlock.Text &= _
String.Format("List(Of Object) assignable from List(Of Integer): {0}", _
genObjListType.IsAssignableFrom(genIntListType)) & vbCrLf
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 produces 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 Object) assignable from List(Of Exception): False
'List(Of Object) assignable from List(Of Integer): False
using System;
using System.Collections.Generic;
class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
// Demonstrate classes:
outputBlock.Text += "Defned Classes:" + "\n";
Room room1 = new Room();
Kitchen kitchen1 = new Kitchen();
Bedroom bedroom1 = new Bedroom();
Guestroom guestroom1 = new Guestroom();
MasterBedroom masterbedroom1 = new MasterBedroom();
Type room1Type = room1.GetType();
Type kitchen1Type = kitchen1.GetType();
Type bedroom1Type = bedroom1.GetType();
Type guestroom1Type = guestroom1.GetType();
Type masterbedroom1Type = masterbedroom1.GetType();
outputBlock.Text += String.Format("room assignable from kitchen: {0}\n",
room1Type.IsAssignableFrom(kitchen1Type));
outputBlock.Text += String.Format("bedroom assignable from guestroom: {0}\n",
bedroom1Type.IsAssignableFrom(guestroom1Type));
outputBlock.Text += String.Format("kitchen assignable from masterbedroom: {0}\n",
kitchen1Type.IsAssignableFrom(masterbedroom1Type));
// Demonstrate arrays:
outputBlock.Text += "\n";
outputBlock.Text += "Integer arrays:\n";
int[] array2 = new int[2];
int[] array10 = new int[10];
int[,] array22 = new int[2, 2];
int[,] array24 = new int[2, 4];
Type array2Type = array2.GetType();
Type array10Type = array10.GetType();
Type array22Type = array22.GetType();
Type array24Type = array24.GetType();
outputBlock.Text += String.Format("int[2] assignable from int[10]: {0}\n",
array2Type.IsAssignableFrom(array10Type));
outputBlock.Text += String.Format("int[2] assignable from int[2,4]: {0}\n",
array2Type.IsAssignableFrom(array24Type));
outputBlock.Text += String.Format("int[2,4] assignable from int[2,2]: {0}\n",
array24Type.IsAssignableFrom(array22Type));
// Demonstrate generics:
outputBlock.Text += "\n";
outputBlock.Text += "Generics:\n";
// Note that "int?[]" is the same as "Nullable<int>[]"
int?[] arrayNull = new int?[10];
List<int> genIntList = new List<int>();
List<object> genObjList = new List<object>();
List<Exception> genExList = new List<Exception>();
Type arrayNullType = arrayNull.GetType();
Type genIntListType = genIntList.GetType();
Type genObjListType = genObjList.GetType();
Type genExListType = genExList.GetType();
outputBlock.Text += String.Format("int[10] assignable from int?[10]: {0}\n",
array10Type.IsAssignableFrom(arrayNullType));
outputBlock.Text += String.Format("List<object> assignable from List<Exception>: {0}\n",
genObjListType.IsAssignableFrom(genExListType));
outputBlock.Text += String.Format("List<object> assignable from List<int>: {0}\n",
genObjListType.IsAssignableFrom(genIntListType));
}
}
class Room
{
}
class Kitchen : Room
{
}
class Bedroom : Room
{
}
class Guestroom : Bedroom
{
}
class MasterBedroom : Bedroom
{
}
/* This code produces the following output:
Defined 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<object> assignable from List<Exception>: false
List<object> assignable from List<int>: false
*/
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.