Type.IsSubclassOf(Type) Metoda

Definice

Určuje, zda aktuální Type je odvozen od zadaného 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, který chcete porovnat s aktuálním typem.

Návraty

true je-li proud Type odvozen od c; jinak , false. Tato metoda také vrátí false , pokud c a aktuální Type jsou rovny.

Implementuje

Atributy

Výjimky

Příklady

Následující příklad vytvoří třídu pojmenovanou Class1 a odvozenou třídu s názvem DerivedC1. Volá metodu IsSubclassOf , která ukazuje, že DerivedC1 je podtřídou 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

Poznámky

Metodu IsSubclassOf můžete volat a určit některou z následujících možností:

  • Určuje, zda jedna třída je odvozena od jiné.

  • Zda typ je odvozen od ValueType. Je to ale efektivnější způsob, jak určit, IsValueType jestli je typ typu hodnota.

  • Zda typ je odvozen od Enum. Metoda IsEnum je ale efektivnější způsob, jak určit, zda je typ výčtem.

  • Zda je typ delegát, to znamená, zda je odvozen z nebo DelegateMulticastDelegate.

Metodu IsSubclassOf nelze použít k určení, zda rozhraní je odvozeno z jiného rozhraní, nebo zda třída implementuje rozhraní. Použijte metodu IsAssignableFrom pro tento účel, jak ukazuje následující příklad.

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

Pokud aktuální Type představuje parametr typu v definici obecného typu nebo obecné metody, je odvozen z omezení třídy nebo z System.Object , pokud nemá žádné omezení třídy.

Note

S výjimkou případů, kdy se používá s rozhraními, IsSubclassOf je naopak IsAssignableFrom. To znamená, že pokud t1.IsSubclassOf(t2) je , pak true je také t2.IsAssignableFrom(t1)true.

Tuto metodu lze přepsat odvozenou třídou.

Platí pro

Viz také