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和衍生的類別,名為DerivedC1The 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.

  • 是否類型是委派,也就是它衍生自其中一個是否DelegateMulticastDelegateWhether 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是反向IsAssignableFromExcept when used with interfaces, IsSubclassOf is the converse of IsAssignableFrom. 也就是說,如果t1.IsSubclassOf(t2)true,然後t2.IsAssignableFrom(t1)也是trueThat is, if t1.IsSubclassOf(t2) is true, then t2.IsAssignableFrom(t1) is also true.

在衍生類別可以覆寫此方法。This method can be overridden by a derived class.

適用於

另請參閱