Type.IsSubclassOf(Type) Type.IsSubclassOf(Type) Type.IsSubclassOf(Type) Type.IsSubclassOf(Type) Method

定义

确定当前 Type 是否派生自指定的 TypeDetermines whether the current Type derives from the specified Type.

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
Public Overridable Function IsSubclassOf (c As Type) As Boolean

参数

c
Type Type Type Type

要与当前类型进行比较的类型。The type to compare with the current type.

返回

如果当前 true 派生于 Type,则为 c;否则为 falsetrue if the current Type derives from c; otherwise, false. 如果 false 和当前 c 相等,此方法也返回 TypeThis method also returns false if c and the current Type are equal.

实现

异常

示例

下面的示例创建一个名为Class1的类和一个名DerivedC1为的派生类。The following example creates a class named Class1 and a derived class named DerivedC1. 它调用IsSubclassOf方法以DerivedC1显示是的子类Class1It calls the IsSubclassOf method to show that DerivedC1 is a subclass of 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
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方法来确定以下任何内容:You can call the IsSubclassOf method to determine any of the following:

  • 一个类是否派生自另一个类。Whether one class derives from another.

  • 类型是否派生自ValueTypeWhether a type derives from ValueType. 不过, IsValueType是一种更有效的方法来确定类型是否为值类型。However, the IsValueType is a more efficient way to determine whether a type is a value type.

  • 类型是否派生自EnumWhether a type derives from Enum. 不过, IsEnum方法是一种更有效的方法来确定类型是否为枚举。However, the IsEnum method is a more efficient way to determine whether a type is an enumeration.

  • 类型是否为委托,即是否从DelegateMulticastDelegate派生。Whether a type is a delegate, that is, whether it derives from either Delegate or MulticastDelegate.

IsSubclassOf方法不能用于确定接口是派生自另一个接口,还是类实现接口。The IsSubclassOf method cannot be used to determine whether an interface derives from another interface, or whether a class implements an interface. IsAssignableFrom使用方法实现此目的,如以下示例所示。Use the IsAssignableFrom method for that purpose, as the following example shows.

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
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派生(如果它没有类约束)。If the current Type represents a type parameter in the definition of a generic type or generic method, it derives from its class constraint or from System.Object if it has no class constraint.

备注

除了与接口一起使用时IsSubclassOf ,与IsAssignableFrom相反。Except when used with interfaces, IsSubclassOf is the converse of IsAssignableFrom. 也就是说,如果t1.IsSubclassOf(t2)true,则t2.IsAssignableFrom(t1)true是。That is, if t1.IsSubclassOf(t2) is true, then t2.IsAssignableFrom(t1) is also true.

此方法可由派生类重写。This method can be overridden by a derived class.

适用于

另请参阅