Type.BaseType プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
現在の Type の直接の継承元である型を取得します。
public:
abstract property Type ^ BaseType { Type ^ get(); };
public abstract Type? BaseType { get; }
public abstract Type BaseType { get; }
member this.BaseType : Type
Public MustOverride ReadOnly Property BaseType As Type
プロパティ値
現在の Type の直接の継承元の Type。現在の null が Type クラスまたはインターフェイスを表す場合は Object。
実装
例
プロパティの使用例を次に示 BaseType します。
using namespace System;
void main()
{
Type^ t = int::typeid;
Console::WriteLine( "{0} inherits from {1}.", t, t->BaseType );
}
using System;
class TestType
{
public static void Main()
{
Type t = typeof(int);
Console.WriteLine("{0} inherits from {1}.", t,t.BaseType);
}
}
Class TestType
Public Shared Sub Main()
Dim t As Type = GetType(Integer)
Console.WriteLine("{0} inherits from {1}.", t, t.BaseType)
End Sub
End Class
次の例では、再帰を使用して、アセンブリ内にある各クラスの完全な継承階層を一覧表示します。 この例では、 という名前のクラスから派生する という名前のクラスを定義します。このクラスは、 という名前のクラス C B から派生します A 。
using System;
public class Example
{
public static void Main()
{
foreach (var t in typeof(Example).Assembly.GetTypes()) {
Console.WriteLine("{0} derived from: ", t.FullName);
var derived = t;
do {
derived = derived.BaseType;
if (derived != null)
Console.WriteLine(" {0}", derived.FullName);
} while (derived != null);
Console.WriteLine();
}
}
}
public class A {}
public class B : A
{}
public class C : B
{}
// The example displays the following output:
// Example derived from:
// System.Object
//
// A derived from:
// System.Object
//
// B derived from:
// A
// System.Object
//
// C derived from:
// B
// A
// System.Object
Public Class Example
Public Shared Sub Main()
For Each t In GetType(Example).Assembly.GetTypes()
Console.WriteLine("{0} derived from: ", t.FullName)
Dim derived As Type = t
Do
derived = derived.BaseType
If derived IsNot Nothing Then
Console.WriteLine(" {0}", derived.FullName)
End If
Loop While derived IsNot Nothing
Console.WriteLine()
Next
End Sub
End Class
Public Class A
End Class
Public Class B : Inherits A
End Class
Public Class C : Inherits B
End Class
' The example displays the following output:
' Example derived from:
' System.Object
'
' A derived from:
' System.Object
'
' B derived from:
' A
' System.Object
'
' C derived from:
' B
' A
' System.Object
注釈
基本型は、現在の型が直接継承する型です。 Object は基本型を持たない唯一の型であるため、 の基本型 null として返されます Object 。
インターフェイスは、0 個以上の基本インターフェイスから継承されます。したがって、 オブジェクトがインターフェイスを null 表す Type 場合、このプロパティは を返します。 基本インターフェイスは、 または を使用して決定 GetInterfaces できます FindInterfaces 。
現在の が Type 構築されたジェネリック型を表す場合、基本型にはジェネリック引数が反映されます。 たとえば、次のような宣言があるとします。
generic<typename U> ref class B { };
generic<typename T> ref class C : B<T> { };
class B<U> { }
class C<T> : B<T> { }
Class B(Of U)
End Class
Class C(Of T)
Inherits B(Of T)
End Class
構築された型 C<int> ( では C(Of Integer) Visual Basic) の場合、 プロパティ BaseType は を返します B<int> 。
現在の がジェネリック型定義の型パラメーターを表している場合は、クラス制約 、つまり、型パラメーターが継承する必要があるクラス Type BaseType を返します。 クラス制約がない場合、 は BaseType を返します System.Object 。
このプロパティは読み取り専用です。