Freigeben über


Type.IsSubclassOf-Methode

Bestimmt, ob die vom aktuellen Type dargestellte Klasse von der Klasse abgeleitet ist, die vom angegebenen Type dargestellt wird.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Overridable Function IsSubclassOf ( _
    c As Type _
) As Boolean
'Usage
Dim instance As Type
Dim c As Type
Dim returnValue As Boolean

returnValue = instance.IsSubclassOf(c)
[ComVisibleAttribute(true)] 
public virtual bool IsSubclassOf (
    Type c
)
[ComVisibleAttribute(true)] 
public:
virtual bool IsSubclassOf (
    Type^ c
)
/** @attribute ComVisibleAttribute(true) */ 
public boolean IsSubclassOf (
    Type c
)
ComVisibleAttribute(true) 
public function IsSubclassOf (
    c : Type
) : boolean

Parameter

  • c
    Der Type, der mit dem aktuellen Type verglichen werden soll.

Rückgabewert

true, wenn der durch den c-Parameter dargestellte Type und der aktuelle Type Klassen darstellen und die durch den aktuellen Type dargestellte Klasse von der durch c dargestellten Klasse abgeleitet ist, andernfalls false. Diese Methode gibt auch dann false zurück, wenn c und der aktuelle Type dieselbe Klasse darstellen.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentNullException

Der c-Parameter ist NULL (Nothing in Visual Basic).

Hinweise

Die IsSubclassOf-Methode kann nicht verwendet werden, um zu bestimmen, ob eine Schnittstelle von einer anderen Schnittstelle abgeleitet ist oder eine Klasse eine Schnittstelle implementiert. Verwenden Sie für diesen Zweck die GetInterface-Methode.

Wenn der aktuelle Type einen Typparameter einer generischen Typ- oder Methodendefinition darstellt, ist er von seiner Klasseneinschränkung oder, falls er keine Klasseneinschränkung besitzt, von System.Object abgeleitet.

Hinweis

In Fällen, in denen IsSubclassOf die Umkehrung von IsAssignableFrom ist. Das heißt, wenn t1.IsSubclassOf(t2)true ist, dann ist t2.IsAssignableFrom(t1) ebenfalls true.

Diese Methode kann von einer abgeleiteten Klasse überschrieben werden.

Beispiel

Das folgende Beispiel veranschaulicht die Verwendung der IsSubclassOf-Methode.

Imports System

Public Interface IMyIfc
End Interface 'IMyIfc

Public Interface IDerived
    Inherits IMyIfc
End Interface 'IDerived

Public Class Class1
    Implements IMyIfc
End Class 'Class1

Public Class MyDerivedClass
    Inherits Class1
End Class 'MyDerivedClass

Class IsSubclassTest

    Public Shared Sub Main()
        Dim imyifcType As Type = GetType(IMyIfc)
        Dim imyderivedType As Type = GetType(IDerived)
        Dim mc As New Class1()
        Dim mcType As Type = mc.GetType()
        Dim mdc = New MyDerivedClass()
        Dim mdcType As Type = mdc.GetType()
        Dim array(10) As Integer
        Dim arrayOfIntsType As Type = array.GetType()
        Dim arrayType As Type = GetType(Array)

        Console.WriteLine("Is Array a derived class of int[]? {0}", arrayType.IsSubclassOf(arrayOfIntsType))
        Console.WriteLine("Is int [] a derived class of Array? {0}", arrayOfIntsType.IsSubclassOf(arrayType))
        Console.WriteLine("Is IMyIfc a derived class of IDerived? {0}", imyifcType.IsSubclassOf(imyderivedType))
        Console.WriteLine("Is myclass a derived class of Class1? {0}", mcType.IsSubclassOf(mcType))
        Console.WriteLine("Is myderivedclass a derived class of Class1? {0}", mdcType.IsSubclassOf(mcType))
    End Sub 'Main
End Class 'IsSubclassTest
using System;
public interface IMyIfc {}
public interface IDerived : IMyIfc {}
public class Class1 : IMyIfc {}
public class MyDerivedClass : Class1 {}
class IsSubclassTest 
{
    public static void Main() 
    {
        Type imyifcType = typeof(IMyIfc);
        Type imyderivedType = typeof(IDerived);
        Class1 mc = new Class1();
        Type mcType = mc.GetType();
        Class1 mdc = new MyDerivedClass();
        Type mdcType = mdc.GetType();
        int [] array  = new int [10];
        Type arrayOfIntsType = array.GetType();
        Type arrayType = typeof(Array);
    
        Console.WriteLine("Is Array a derived class of int[]? {0}", arrayType.IsSubclassOf(arrayOfIntsType));
        Console.WriteLine("Is int [] a derived class of Array? {0}", arrayOfIntsType.IsSubclassOf(arrayType));
        Console.WriteLine("Is IMyIfc a derived class of IDerived? {0}", imyifcType.IsSubclassOf(imyderivedType));
        Console.WriteLine("Is myclass a derived class of Class1? {0}", mcType.IsSubclassOf(mcType));
        Console.WriteLine("Is myderivedclass a derived class of Class1? {0}", mdcType.IsSubclassOf(mcType));
    }
}
using namespace System;
interface class IMyIfc{};
interface class IDerived: public IMyIfc{};
public ref class Class1: public IMyIfc{};

public ref class MyDerivedClass: public Class1{};

int main()
{
   Type^ imyifcType = IMyIfc::typeid;
   Type^ imyderivedType = IDerived::typeid;
   Class1^ mc = gcnew Class1;
   Type^ mcType = mc->GetType();
   Class1^ mdc = gcnew MyDerivedClass;
   Type^ mdcType = mdc->GetType();
   array<Int32>^arr = gcnew array<Int32>(10);
   Type^ arrayOfIntsType = arr->GetType();
   Type^ arrayType = Array::typeid;
   Console::WriteLine( "Is Array a derived class of Int32[]? {0}", arrayType->IsSubclassOf( arrayOfIntsType ).ToString() );
   Console::WriteLine( "Is Int32[] a derived class of Array? {0}", arrayOfIntsType->IsSubclassOf( arrayType ).ToString() );
   Console::WriteLine( "Is IMyIfc a derived class of IDerived? {0}", imyifcType->IsSubclassOf( imyderivedType ).ToString() );
   Console::WriteLine( "Is myclass a derived class of Class1? {0}", mcType->IsSubclassOf( mcType ).ToString() );
   Console::WriteLine( "Is myderivedclass a derived class of Class1? {0}", mdcType->IsSubclassOf( mcType ).ToString() );
}
import System.*;

public interface IMyIfc
{
} //IMyIfc

public interface IDerived extends IMyIfc
{
} //IDerived

public class Class1 implements IMyIfc
{
} //Class1

public class MyDerivedClass extends Class1
{
} //MyDerivedClass

class IsSubclassTest
{
    public static void main(String[] args)
    {
        Type iMyIfcType = IMyIfc.class.ToType();
        Type iMyDerivedType = IDerived.class.ToType();
        Class1 mc = new Class1();
        Type mcType = mc.GetType();
        Class1 mdc = new MyDerivedClass();
        Type mdcType = mdc.GetType();
        int array[] = new int[10];
        Type arrayOfIntsType = array.GetType();
        Type arrayType = Array.class.ToType();

        Console.WriteLine("Is Array a derived class of int[]? {0}",
            System.Convert.ToString(arrayType.IsSubclassOf(arrayOfIntsType)));
        Console.WriteLine("Is int [] a derived class of Array? {0}", 
            System.Convert.ToString(arrayOfIntsType.IsSubclassOf(arrayType)));
        Console.WriteLine("Is IMyIfc a derived class of IDerived? {0}", 
            System.Convert.ToString(iMyIfcType.IsSubclassOf(iMyDerivedType)));
        Console.WriteLine("Is myclass a derived class of Class1? {0}", 
            System.Convert.ToString(mcType.IsSubclassOf(mcType)));
        Console.WriteLine("Is myderivedclass a derived class of Class1? {0}", 
            System.Convert.ToString(mdcType.IsSubclassOf(mcType)));
    } //main
} //IsSubclassTest

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Type-Klasse
Type-Member
System-Namespace
BaseType