Type.IsSubclassOf(Type) Metoda

Definicja

Określa, czy bieżący pochodzi Type z określonego 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

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 .

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

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.

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

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

Zobacz też