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自名為的類別, 而後者又衍生自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.

介面繼承自零或多個基底介面;因此, null Type如果物件代表介面, 則這個屬性會傳回。Interfaces 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>C(Of Integer)別 ( BaseType在 Visual Basic 中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.Object則會傳回。If there is no class constraint, BaseType returns System.Object.

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

適用於

另請參閱