Type.IsSubclassOf(Type) 方法

定义

确定当前 Type 是否派生自指定的 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

参数

c
Type

要与当前类型进行比较的类型。

返回

如果当前 true 派生于 Type,则为 c;否则为 false。 如果 false 和当前 c 相等,此方法也返回 Type

实现

属性

例外

cnull

示例

以下示例创建名为 的 Class1 类和名为 的 DerivedC1派生类。 它调用 IsSubclassOf 方法以显示 DerivedC1 是 的 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

注解

可以调用 IsSubclassOf 方法来确定以下任一项:

  • 一个类是否派生自另一个类。

  • 类型是否派生自 ValueType。 但是, IsValueType 是确定类型是否为值类型的更有效方法。

  • 类型是否派生自 Enum。 但是,方法 IsEnum 是确定类型是否为枚举的更有效方法。

  • 无论类型是委托,即它派生自 Delegate 还是 MulticastDelegate

方法 IsSubclassOf 不能用于确定接口是从另一个接口派生的,还是类是否实现接口。 IsAssignableFrom为此,请使用 方法,如以下示例所示。

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

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

备注

与 接口一起使用时除外, IsSubclassOf 相反 IsAssignableFrom。 也就是说,如果 t1.IsSubclassOf(t2)true,则 t2.IsAssignableFrom(t1) 也是 true

派生类可以重写此方法。

适用于

另请参阅