Type.BaseType 属性

定义

获取当前 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);
    }
}
let t = typeof<int>
printfn $"{t} inherits from {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
type A() = class end 

type B() = inherit A()

type C() = inherit B()   

module Example =
    [<EntryPoint>]
    let main _ =
        for t in typeof<A>.Assembly.GetTypes() do
            printfn $"{t.FullName} derived from: "
            let mutable derived = t
            while derived <> null do
                derived <- derived.BaseType
                if derived <> null then 
                    printfn $"   {derived.FullName}"
            printfn ""
        0
// 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

接口继承自零个或多个基接口;因此,如果 对象表示接口,Type则此属性返回 null 。 可以使用 或 FindInterfaces确定GetInterfaces基接口。

如果当前 Type 表示构造的泛型类型,则基类型反映泛型参数。 以下面的声明为例:

generic<typename U> ref class B { };
generic<typename T> ref class C : B<T> { };
class B<U> { }
class C<T> : B<T> { }
type B<'U>() = class end
type C<'T>() = inherit B<'T>()
Class B(Of U)
End Class
Class C(Of T)
    Inherits B(Of T)
End Class

对于 Visual Basic) C(Of Integer) 中的构造类型C<int> (,BaseType属性返回 B<int>

如果当前 Type 表示泛型类型定义的类型参数, BaseType 则返回类约束,即类型参数必须继承的类。 如果没有类约束, BaseTypeSystem.Object返回 。

此属性为只读。

适用于

另请参阅