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.

  • 類型是否衍生ValueType自。Whether a type derives from ValueType. 不過,若IsValueType要判斷型別是否為實值型別,則是更有效率的方式。However, the IsValueType is a more efficient way to determine whether a type is a value type.

  • 類型是否衍生Enum自。Whether a type derives from Enum. 不過, IsEnum方法是判斷類型是否為列舉的更有效率方式。However, the IsEnum method is a more efficient way to determine whether a type is an enumeration.

  • 型別是否為委派,也就是它是否衍生自Delegate或。 MulticastDelegateWhether 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. 針對該目的使用方法,如下列範例所示。IsAssignableFromUse 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)truet2.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.

適用於

另請參閱