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.

适用于

另请参阅