Type.IsSubclassOf(Type) Yöntem

Tanım

Geçerli Type değerin belirtilen Type'den türetilip türemediğini belirler.

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

Parametreler

c
Type

Geçerli türle karşılaştıracak tür.

Döndürülenler

true geçerli Type öğesinin ctüretilirse; değilse, false. Bu yöntem ayrıca ve geçerli Type değerin eşit olup olmadığını c da döndürürfalse.

Uygulamalar

Öznitelikler

Özel durumlar

c, null değeridir.

Örnekler

Aşağıdaki örnek adlı bir sınıf ve adlı Class1DerivedC1türetilmiş bir sınıf oluşturur. öğesinin IsSubclassOf bir alt sınıfı Class1olduğunu DerivedC1 göstermek için yöntemini çağırır.

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

Açıklamalar

Aşağıdakilerden birini belirlemek için yöntemini çağırabilirsiniz IsSubclassOf :

  • Bir sınıfın başka bir sınıftan türetilip türemediği.

  • Bir türün türünden ValueTypetüretilip türetilmediği. Ancak, IsValueType türün bir değer türü olup olmadığını saptamak için daha verimli bir yoldur.

  • Bir türün türünden Enumtüretilip türetilmediği. Ancak yöntemi, bir türün IsEnum sabit listesi olup olmadığını saptamak için daha verimli bir yoldur.

  • Türün temsilci olup olmadığı, diğer bir ifadeyle veya MulticastDelegatetüründen Delegate türetilip türetilmediği.

yöntemi, bir arabirimin IsSubclassOf başka bir arabirimden türetilip türetilmediğini veya bir sınıfın arabirim uygulayıp uygulamadığını belirlemek için kullanılamaz. IsAssignableFrom Aşağıdaki örnekte gösterildiği gibi yöntemini bu amaç için kullanın.

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

Current Type , genel bir türün veya genel yöntemin tanımındaki bir tür parametresini temsil ederse, sınıf kısıtlamasından veya sınıf kısıtlaması yoksa türünden System.Object türetilir.

Not

Arabirimlerle kullanıldığı durumlar dışında, IsSubclassOf öğesinin IsAssignableFromtersidir. Yani, ise t1.IsSubclassOf(t2)truede t2.IsAssignableFrom(t1)trueolur.

Bu yöntem türetilmiş bir sınıf tarafından geçersiz kılınabilir.

Şunlara uygulanır

Ayrıca bkz.