共用方式為


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

屬性值

電流直接繼承的 TypeTypenull電流Type代表Object類別或介面。

實作

範例

以下範例示範如何使用該 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 代表一個構造化的泛型,則基底型別會反映泛型參數。 例如,請考慮下列宣告:

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

對於建構型 C<int> 別(C(Of Integer) 在 Visual Basic 中),屬性 BaseType 會回傳 B<int>

若 代表 Type 一般型別定義的型態參數, BaseType 則回傳類別約束,也就是型別參數必須繼承的類別。 若無類別限制,則返回 BaseTypeSystem.Object

這個屬性是唯讀的。

適用於

另請參閱