Bagikan melalui


Type.BaseType Properti

Definisi

Mendapatkan jenis dari mana saat ini Type langsung mewarisi.

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

Nilai Properti

dari Type mana saat ini Type langsung mewarisi, atau null jika saat ini Type mewakili Object kelas atau antarmuka.

Penerapan

Contoh

Contoh berikut menunjukkan menggunakan BaseType properti .

using System;
class TestType
{
    public static void Main()
    {
        Type t = typeof(int);
        Console.WriteLine("{0} inherits from {1}.", t,t.BaseType);
    }
}
let t = typeof<int>
printfn $"{t} inherits from {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

Contoh berikut menggunakan rekursi untuk mencantumkan hierarki pewarisan lengkap dari setiap kelas yang ditemukan dalam rakitan. Contoh mendefinisikan kelas bernama C yang berasal dari kelas bernama B, yang, pada gilirannya, berasal dari kelas bernama 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
type A() = class end 

type B() = inherit A()

type C() = inherit B()   

module Example =
    [<EntryPoint>]
    let main _ =
        for t in typeof<A>.Assembly.GetTypes() do
            printfn $"{t.FullName} derived from: "
            let mutable derived = t
            while derived <> null do
                derived <- derived.BaseType
                if derived <> null then 
                    printfn $"   {derived.FullName}"
            printfn ""
        0
// 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

Keterangan

Jenis dasar adalah jenis dari mana jenis saat ini langsung mewarisi. Objectadalah satu-satunya jenis yang tidak memiliki jenis dasar, oleh karena itu null dikembalikan sebagai jenis dasar .Object

Antarmuka mewarisi dari nol atau lebih antarmuka dasar; oleh karena itu, properti ini mengembalikan null jika Type objek mewakili antarmuka. Antarmuka dasar dapat ditentukan dengan GetInterfaces atau FindInterfaces.

Jika saat ini Type mewakili jenis generik yang dibangun, jenis dasar mencerminkan argumen generik. Misalnya, pertimbangkan deklarasi berikut:

class B<U> { }
class C<T> : B<T> { }
type B<'U>() = class end
type C<'T>() = inherit B<'T>()
Class B(Of U)
End Class
Class C(Of T)
    Inherits B(Of T)
End Class

Untuk jenis C<int> yang dibangun (C(Of Integer) di Visual Basic), BaseType properti mengembalikan B<int>.

Jika saat ini Type mewakili parameter jenis definisi jenis generik, BaseType mengembalikan batasan kelas, yaitu, kelas parameter jenis harus mewarisi. Jika tidak ada batasan kelas, BaseType mengembalikan System.Object.

Properti ini hanya dapat dibaca.

Berlaku untuk

Lihat juga