Type.IsSubclassOf(Type) Metoda

Definicja

Określa, czy bieżący pochodzi Type z określonego Type .

C#
public virtual bool IsSubclassOf (Type c);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public virtual bool IsSubclassOf (Type c);

Parametry

c
Type

Typ do porównania z bieżącym typem.

Zwraca

Boolean

true Jeśli bieżący pochodzi Type z ; w przeciwnym razie c false . Ta metoda zwraca również false wartość c if, a bieżąca Type wartość jest równa.

Implementuje

Atrybuty

Wyjątki

Przykłady

Poniższy przykład tworzy klasę o Class1 nazwie i klasę pochodną o nazwie DerivedC1 . Wywołuje metodę IsSubclassOf , aby pokazać, że jest DerivedC1 podklasą Class1 .

C#
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

Uwagi

Możesz wywołać metodę IsSubclassOf , aby określić dowolną z następujących czynności:

  • Czy jedna klasa pochodzi od innej.

  • Określa, czy typ pochodzi od ValueType typu . Jednak jest IsValueType to bardziej wydajny sposób określania, czy typ jest typem wartości.

  • Określa, czy typ pochodzi od Enum typu . Jednak metoda IsEnum jest bardziej wydajnym sposobem określania, czy typ jest wyliczeniem.

  • Określa, czy typ jest delegatem, czyli czy pochodzi od typu Delegate lub MulticastDelegate .

Metoda nie może służyć do określenia, czy interfejs pochodzi z innego interfejsu, czy też klasa IsSubclassOf implementuje interfejs. W tym IsAssignableFrom celu użyj metody , jak pokazano w poniższym przykładzie.

C#
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

Jeśli bieżący reprezentuje parametr typu w definicji typu ogólnego lub metody ogólnej, pochodzi od jego ograniczenia klasy lub z, jeśli nie ma żadnych Type System.Object ograniczeń klasy.

Uwaga

Z wyjątkiem sytuacji, gdy jest używany z interfejsami, IsSubclassOf jest odwrotna . IsAssignableFrom Oznacza to, że jeśli t1.IsSubclassOf(t2) to , to jest również true t2.IsAssignableFrom(t1) true .

Metoda ta może być zastąpiona przez klasę pochodną.

Dotyczy

Produkt Wersje
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

Zobacz też