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)在 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.

适用于

另请参阅