Type.IsAssignableFrom Method (Type)

 

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

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

abstract IsAssignableFrom : 
        c:Type -> bool
override IsAssignableFrom : 
        c:Type -> bool

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

    No code example is currently available or this language may not be supported.
  • 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.

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.

No code example is currently available or this language may not be supported.

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:

No code example is currently available or this language may not be supported.

This method can be overridden by a derived class.

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

No code example is currently available or this language may not be supported.

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

No code example is currently available or this language may not be supported.

.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show: