Export (0) Print
Expand All

Type.IsAssignableFrom Method

Determines whether an instance of the current Type can be assigned from an instance of the specified Type.

[Visual Basic]
Public Overridable Function IsAssignableFrom( _
   ByVal c As Type _
) As Boolean
[C#]
public virtual bool IsAssignableFrom(
 Type c
);
[C++]
public: virtual bool IsAssignableFrom(
 Type* c
);
[JScript]
public function IsAssignableFrom(
   c : Type
) : Boolean;

Parameters

c
The Type to compare with the current Type.

Return Value

true if the c parameter 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 supports. false if none of these conditions are the case, or if c is a null reference (Nothing in Visual Basic).

Remarks

This method can be overridden by a derived class.

Determine the element types of a Type using GetElementType.

Example

[Visual Basic, C#, C++] The following example demonstrates the IsAssignable method using arrays.

[Visual Basic] 
Imports System

Class ArrayTypeTest

    Public Shared Sub Main()
        Dim i As Integer = 1
        Dim array10(10) As Integer
        Dim array2(2) As Integer
        Dim array22(2, 2) As Integer
        Dim array24(2, 4) As Integer
        Dim array333(3, 3, 3) 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()
        Dim array333Type As Type = array333.GetType()

        ' If X and Y are not both arrays, then return false.
        Console.WriteLine("Is int[2] assignable from int? {0}.", array2Type.IsAssignableFrom(i.GetType()))
        ' If X and Y have same type and rank, then return true.
        Console.WriteLine("Is int[2] assignable from int[10]? {0}.", array2Type.IsAssignableFrom(array10Type))
        Console.WriteLine("Is int[2,2] assignable from int[2,4]? {0}.", array22Type.IsAssignableFrom(array24Type))
        Console.WriteLine("Is int[2,4] assignable from int[2,2]? {0}.", array24Type.IsAssignableFrom(array22Type))
        ' If X and Y do not have the same rank, then return false.
        Console.WriteLine("Is int[2,2] assignable from int[10]? {0}.", array22Type.IsAssignableFrom(array10Type))
        Console.WriteLine("Is int[2,2] assignable from int[3,3,3]? {0}.", array22Type.IsAssignableFrom(array333Type))
        Console.WriteLine("Is int[3,3,3] is assignable from int[2,2]? {0}.", array333Type.IsAssignableFrom(array22Type))
    End Sub 'Main
End Class 'ArrayTypeTest

[C#] 
using System;
class ArrayTypeTest 
{
    public static void Main() 
    {
        int i = 1;
        int [] array10 = new int [10];
        int [] array2 = new int[2];
        int [,]array22 = new int[2,2];
        int [,]array24 = new int[2,4];
        int [,,]array333 = new int[3,3,3];
        Type array10Type = array10.GetType();
        Type array2Type = array2.GetType();
        Type array22Type = array22.GetType();
        Type array24Type = array24.GetType();
        Type array333Type = array333.GetType();

        // If X and Y are not both arrays, then return false.
        Console.WriteLine("Is int[2] assignable from int? {0}.", array2Type.IsAssignableFrom(i.GetType()));
        // If X and Y have same type and rank, then return true.
        Console.WriteLine("Is int[2] assignable from int[10]? {0}.", array2Type.IsAssignableFrom(array10Type));
        Console.WriteLine("Is int[2,2] assignable from int[2,4]? {0}.", array22Type.IsAssignableFrom(array24Type));
        Console.WriteLine("Is int[2,4] assignable from int[2,2]? {0}.", array24Type.IsAssignableFrom(array22Type));
        // If X and Y do not have the same rank, then return false.
        Console.WriteLine("Is int[2,2] assignable from int[10]? {0}.", array22Type.IsAssignableFrom(array10Type));
        Console.WriteLine("Is int[2,2] assignable from int[3,3,3]? {0}.", array22Type.IsAssignableFrom(array333Type));
        Console.WriteLine("Is int[3,3,3] assignable from int[2,2]? {0}.", array333Type.IsAssignableFrom(array22Type));
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

int main() {
   Int32 i = 1;
   Int32 array10[] = new Int32[10];
   Int32 array2[] = new Int32[2];
   Int32 array22[,] = new Int32[2, 2];
   Int32 array24[,] = new Int32[2, 4];
   Int32 array333[,,] = new Int32[3, 3, 3];
   Type*  array10Type = array10->GetType();
   Type*  array2Type = array2->GetType();
   Type*  array22Type = array22->GetType();
   Type*  array24Type = array24->GetType();
   Type*  array333Type = array333->GetType();

   // If X and Y are not both arrays, then return false.
   Console::WriteLine(S"Is Int32[2] assignable from Int32? {0}.", array2Type->IsAssignableFrom(__box(i)->GetType()).ToString());
   // If X and Y have same type and rank, then return true.
   Console::WriteLine(S"Is Int32[2] assignable from Int32[10]? {0}.", array2Type->IsAssignableFrom(array10Type).ToString());
   Console::WriteLine(S"Is Int32[2,2] assignable from Int32[2,4]? {0}.", array22Type->IsAssignableFrom(array24Type).ToString());
   Console::WriteLine(S"Is Int32[2,4] assignable from Int32[2,2]? {0}.", array24Type->IsAssignableFrom(array22Type).ToString());
   // If X and Y do not have the same rank, then return false.
   Console::WriteLine(S"Is Int32[2,2] assignable from Int32[10]? {0}.", array22Type->IsAssignableFrom(array10Type).ToString());
   Console::WriteLine(S"Is Int32[2,2] assignable from Int32[3,3,3]? {0}.", array22Type->IsAssignableFrom(array333Type).ToString());
   Console::WriteLine(S"Is Int32[3,3,3] assignable from int[2,2]? {0}.", array333Type->IsAssignableFrom(array22Type).ToString());
}

[Visual Basic, C#, C++] This code produces the following output:

int[2] is assignable from int? False
    int[2] is assignable from int[10]? True
    int[2,2] is assignable from int[2,4]? True
    int[2,4] is assignable from int[2,2]? True
    int[2,2] is assignable from int[10]? False
    int[2,2] is assignable from int[3,3,3]? False
    int[3,3,3] is assignable from int[2,2]? False

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Type Class | Type Members | System Namespace

Show:
© 2014 Microsoft