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)
abstract IsAssignableFrom : c:Type -> bool override IsAssignableFrom : c:Type -> bool
The type to compare with the current type.
Return ValueType: 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 indicates that an instance of the generic type parameter can be assigned to an Stream object.
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.
Themethod 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.
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.
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.
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0