Bahasa

Type.IsSubclassOf(Type) Metode

Definisi

Menentukan apakah saat ini Type berasal dari yang ditentukan Type.

public:
 virtual bool IsSubclassOf(Type ^ c);
public virtual bool IsSubclassOf(Type c);
[System.Runtime.InteropServices.ComVisible(true)]
public virtual bool IsSubclassOf(Type c);
abstract member IsSubclassOf : Type -> bool
override this.IsSubclassOf : Type -> bool
[<System.Runtime.InteropServices.ComVisible(true)>]
abstract member IsSubclassOf : Type -> bool
override this.IsSubclassOf : Type -> bool
Public Overridable Function IsSubclassOf (c As Type) As Boolean

Parameter

c
Type

Jenis untuk dibandingkan dengan jenis saat ini.

Mengembalikan

true jika saat ini Type berasal dari c; jika tidak, false. Metode ini juga mengembalikan false jika c dan saat ini Type sama.

Penerapan

Atribut

Pengecualian

c adalah null.

Contoh

Contoh berikut membuat kelas bernama Class1 dan kelas turunan bernama DerivedC1. Ini memanggil IsSubclassOf metode untuk menunjukkan bahwa DerivedC1 adalah subkelas dari Class1.

using System;

public class Class1 { }
public class DerivedC1 : Class1 { }

class IsSubclassTest
{
   public static void Main()
   {
      Console.WriteLine("DerivedC1 subclass of Class1: {0}",
                         typeof(DerivedC1).IsSubclassOf(typeof(Class1)));
   }
}
// The example displays the following output:
//        DerivedC1 subclass of Class1: True
type Class1() = class end
type DerivedC1() = inherit Class1()

printfn $"DerivedC1 subclass of Class1: {typeof<DerivedC1>.IsSubclassOf typeof<Class1>}"

// The example displays the following output:
//        DerivedC1 subclass of Class1: True
Public Class Class1
End Class

Public Class DerivedC1 : Inherits Class1
End Class

Public Module Example
   Public Sub Main()
      Console.WriteLine("DerivedC1 subclass of Class1: {0}",
                         GetType(DerivedC1).IsSubClassOf(GetType(Class1)))
   End Sub
End Module
' The example displays the following output:
'       DerivedC1 subclass of Class1: True

Keterangan

Anda dapat memanggil IsSubclassOf metode untuk menentukan salah satu hal berikut:

  • Apakah satu kelas berasal dari kelas lain.

  • Apakah jenis berasal dari ValueType. Namun, IsValueType adalah cara yang lebih efisien untuk menentukan apakah jenis adalah jenis nilai.

  • Apakah jenis berasal dari Enum. Namun, IsEnum metode ini adalah cara yang lebih efisien untuk menentukan apakah jenis adalah enumerasi.

  • Apakah jenis adalah delegasi, yaitu, apakah itu berasal dari Delegate atau MulticastDelegate.

Metode IsSubclassOf tidak dapat digunakan untuk menentukan apakah antarmuka berasal dari antarmuka lain, atau apakah kelas mengimplementasikan antarmuka. Gunakan metode untuk tujuan tersebut IsAssignableFrom , seperti yang ditunjukkan contoh berikut.

using System;

public interface IInterface
{
   void Display();
}

public class Implementation : IInterface
{
   public void Display()
   {
      Console.WriteLine("The implementation...");
   }
}

public class Example
{
   public static void Main()
   {
      Console.WriteLine("Implementation is a subclass of IInterface:   {0}",
                        typeof(Implementation).IsSubclassOf(typeof(IInterface)));
      Console.WriteLine("IInterface is assignable from Implementation: {0}",
                        typeof(IInterface).IsAssignableFrom(typeof(Implementation)));
   }
}
// The example displays the following output:
//       Implementation is a subclass of IInterface:   False
//       IInterface is assignable from Implementation: True
type IInterface =
    abstract Display : unit -> unit

type Implementation() =
    interface IInterface with
        member _.Display() = printfn "The implementation..."

printfn $"Implementation is a subclass of IInterface:   {typeof<Implementation>.IsSubclassOf typeof<IInterface>}"
printfn $"IInterface is assignable from Implementation: {typeof<IInterface>.IsAssignableFrom typeof<Implementation>}"
// The example displays the following output:
//       Implementation is a subclass of IInterface:   False
//       IInterface is assignable from Implementation: True
Public Interface IInterface
   Sub Display()
End Interface

Public Class Implementation : Implements IInterface
   Public Sub Display() _
      Implements IInterface.Display

      Console.WriteLine("The implementation...")
   End Sub
End Class

Module Example
   Public Sub Main()
      Console.WriteLine("Implementation is a subclass of IInterface:   {0}",
                        GetType(Implementation).IsSubclassOf(GetType(IInterface)))
      Console.WriteLine("IInterface is assignable from Implementation: {0}",
                        GetType(IInterface).IsAssignableFrom(GetType(Implementation)))
   End Sub
End Module
' The example displays the following output:
'       Implementation is a subclass of IInterface:   False
'       IInterface is assignable from Implementation: True

Jika saat ini Type mewakili parameter jenis dalam definisi jenis generik atau metode generik, parameter tersebut berasal dari batasan kelasnya atau dari System.Object jika tidak memiliki batasan kelas.

Note

Kecuali ketika digunakan dengan antarmuka, IsSubclassOf adalah kebalikan dari IsAssignableFrom. Artinya, jika t1.IsSubclassOf(t2) adalah true, maka t2.IsAssignableFrom(t1) juga true.

Metode ini dapat ditimpa oleh kelas turunan.

Berlaku untuk

Lihat juga