Type.BaseType Właściwość

Definicja

Pobiera typ, z którego dziedziczy Type bezpośrednio bieżący.

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

Wartość właściwości

Type

Element, Type z którego bieżący dziedziczy bezpośrednio, lub jeśli bieżący reprezentuje Type null Type Object klasę lub interfejs.

Implementuje

Przykłady

W poniższym przykładzie pokazano użycie BaseType właściwości .

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

W poniższym przykładzie użyto rekursji, aby wyświetlić pełną hierarchię dziedziczenia każdej klasy znalezionej w zestawie. W przykładzie zdefiniowano klasę o nazwie pochodzącą od klasy o nazwie , która z kolei pochodzi C od klasy o nazwie B 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

Uwagi

Typ podstawowy to typ, z którego bieżący typ bezpośrednio dziedziczy. Object jest jedynym typem, który nie ma typu podstawowego, dlatego null jest zwracany jako typ podstawowy Object .

Interfejsy dziedziczą z zera lub większej liczby interfejsów podstawowych; w związku z tym ta właściwość zwraca null wartość , jeśli obiekt reprezentuje Type interfejs. Interfejsy podstawowe można określić za pomocą GetInterfaces lub FindInterfaces .

Jeśli bieżący reprezentuje Type skonstruowany typ ogólny, typ podstawowy odzwierciedla argumenty ogólne. Rozważmy na przykład następujące deklaracje:

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

Dla skonstruowanego typu C<int> ( C(Of Integer) w Visual Basic) właściwość zwraca wartość BaseType B<int> .

Jeśli bieżący reprezentuje parametr typu definicji typu ogólnego, zwraca ograniczenie klasy, czyli klasę, która musi dziedziczyć Type BaseType parametr typu. Jeśli nie ma ograniczenia klasy, zwraca BaseType wartość System.Object .

Ta właściwość jest tylko do odczytu.

Dotyczy

Zobacz też