Type.BaseType 屬性

定義

取得目前 Type 所直接繼承的類型。Gets the type from which the current Type directly inherits.

public:
 abstract property Type ^ BaseType { Type ^ get(); };
public abstract Type BaseType { get; }
member this.BaseType : Type
Public MustOverride ReadOnly Property BaseType As Type

屬性值

目前 Type 直接繼承自的 Type,如果目前 null 表示 Type 類別或介面,則為 ObjectThe Type from which the current Type directly inherits, or null if the current Type represents the Object class or an interface.

實作

範例

下列範例示範如何使用 BaseType 屬性。The following example demonstrates using the BaseType property.

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

下列範例會使用遞迴來列出在元件中找到之每個類別的完整繼承階層架構。The following example uses recursion to list the complete inheritance hierarchy of each class found in an assembly. 這個範例會定義一個名為 C 的類別,此類別是從名為 B的類別衍生而來,而後者又衍生自名為 A的類別。The example defines a class named C that derives from a class named B, which, in turn, derives from a class named 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

備註

基底型別是目前型別直接繼承的來源型別。The base type is the type from which the current type directly inherits. Object 是唯一不具有基底類型的型別,因此 null 會當做 Object的基底型別傳回。Object is the only type that does not have a base type, therefore null is returned as the base type of Object.

介面繼承自零或多個基底介面;因此,如果 Type 物件代表介面,這個屬性會傳回 nullInterfaces inherit from zero or more base interfaces; therefore, this property returns null if the Type object represents an interface. 您可以使用 GetInterfacesFindInterfaces來判斷基底介面。The base interfaces can be determined with GetInterfaces or FindInterfaces.

如果目前的 Type 代表結構化的泛型型別,則基底型別會反映泛型引數。If the current Type represents a constructed generic type, the base type reflects the generic arguments. 例如,請考慮下列宣告:For example, consider the following declarations:

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> (Visual Basic 中的C(Of Integer)),BaseType 屬性會傳回 B<int>For the constructed type C<int> (C(Of Integer) in Visual Basic), the BaseType property returns B<int>.

如果目前 Type 代表泛型型別定義的型別參數,BaseType 會傳回類別條件約束,也就是型別參數必須繼承的類別。If the current Type represents a type parameter of a generic type definition, BaseType returns the class constraint, that is, the class the type parameter must inherit. 如果沒有類別條件約束,BaseType 會傳回 System.ObjectIf there is no class constraint, BaseType returns System.Object.

這個屬性是唯讀的。This property is read-only.

適用於

另請參閱