共用方式為


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 方法是更有效率的方式,可判斷類型是否為列舉。

  • 類型是否為委派,亦即,不論其是否衍生自 DelegateMulticastDelegate

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

這個方法可由衍生類別覆寫。

適用於

另請參閱