Type.BaseType Type.BaseType Type.BaseType Type.BaseType Property

定義

取得目前 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);
    }
}
Imports System

Class TestType
   
    Public Shared Sub Main()
        Dim t As Type = GetType(Integer)
        Console.WriteLine("{0} inherits from {1}.", t, t.BaseType)
    End Sub 'Main
End Class 'TestType

下列範例會使用遞迴來列出每個類別的組件中找到完整的繼承階層架構。The following example uses recursion to list the complete inheritance hierarchy of each class found in an assembly. 此範例會定義名為類別C衍生自類別,名為B,它會接著衍生自類別,名為AThe 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會傳回基底型別ObjectObject is the only type that does not have a base type, therefore null is returned as the base type of Object.

介面繼承自零或多個基底介面。因此,這個屬性會傳回null如果Type物件表示的介面。Interfaces inherit from zero or more base interfaces; therefore, this property returns null if the Type object represents an interface. 基底介面可以判斷與GetInterfacesFindInterfacesThe 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>(C(Of Integer) Visual Basic 中),則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.

適用於

另請參閱