Type.IsSubclassOf 方法

确定当前 Type 表示的类是否是从指定的 Type 表示的类派生的。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
Public Overridable Function IsSubclassOf ( _
    c As Type _
) As Boolean
用法
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

参数

  • c
    与当前的 Type 进行比较的 Type

返回值

如果 Type 由 c 参数表示并且当前的 Type 表示类,并且当前的 Type 所表示的类是从 c 所表示的类派生的,则为 true;否则为 false。如果 c 和当前的 Type 表示相同的类,则此方法还返回 false

异常

异常类型 条件

ArgumentNullException

c 参数为 空引用(在 Visual Basic 中为 Nothing)。

备注

IsSubclassOf 方法不能用于确定某个接口是否派生自另一个接口或某个类是否实现了某个接口。对于此目的,应该使用 GetInterface 方法。

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则它派生自其类约束;如果它没有类约束,则派生自 System.Object

提示

如果 IsSubclassOfIsAssignableFrom 的逆反。也就是说,如果 t1.IsSubclassOf(t2)true,则 t2.IsAssignableFrom(t1) 也为 true

此方法可由派生类重写。

示例

下面的示例说明 IsSubclassOf 方法的使用。

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

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Type 类
Type 成员
System 命名空间
BaseType