次の方法で共有


Type.IsAssignableFrom メソッド

指定した Type のインスタンスを現在の Type のインスタンスに代入できるかどうかを判断します。

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

パラメータ

  • c
    現在の Type と比較する Type。

戻り値

c パラメータと現在の Type が同じ型を表している場合、現在の Type が c の継承階層にある場合、または現在の Type が c によってサポートされるインターフェイスである場合は true 。これらの条件のいずれも該当しない場合、または c が null 参照 (Visual Basic では Nothing) である場合は false

解説

このメソッドは、派生クラスでオーバーライドできます。

GetElementType を使用して、Type の要素型を判別します。

使用例

[Visual Basic, C#, C++] 配列を使用する IsAssignable メソッドの例を次に示します。

 
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++] このコードによって、次の出力が生成されます。

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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Type クラス | Type メンバ | System 名前空間