共用方式為


Type.IsSubclassOf Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Determines whether the class represented by the current Type derives from the class represented by the specified Type.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Overridable Function IsSubclassOf ( _
    c As Type _
) As Boolean
[ComVisibleAttribute(true)]
public virtual bool IsSubclassOf(
    Type c
)

Parameters

  • c
    Type: System.Type
    The Type to compare with the current Type.

Return Value

Type: System.Boolean
true if the Type represented by the c parameter and the current Type represent classes, and the class represented by the current Type derives from the class represented by c; otherwise, false. This method also returns false if c and the current Type represent the same class.

Exceptions

Exception Condition
ArgumentNullException

The c parameter is nulla null reference (Nothing in Visual Basic).

Remarks

The IsSubclassOf method cannot be used to determine whether an interface derives from another interface, or whether a class implements an interface. Use the GetInterface method for that purpose. Note that if a type is dervived from an interface, this method returns true for that type being a subclass of 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.

NoteNote:

If the IsSubclassOf is the converse of IsAssignableFrom. That is, if t1.IsSubclassOf(t2) is true, then t2.IsAssignableFrom(t1) is also true.

This method can be overridden by a derived class.

Examples

The following example demonstrates the use of the IsSubclassOf method by creating an instance of a class and an instance of its derived class.


Public Class Class1
End Class

Public Class DerivedC1
   Inherits Class1
End Class

Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim myC1 As Class1 = New Class1
      Dim myDClass As DerivedC1 = New DerivedC1
      Dim myClassType As Type = myC1.GetType
      Dim myDClassType As Type = myDClass.GetType

      ' Returns true:
      outputBlock.Text += String.Format("myDClass subclass of myClass: {0}", myDClassType.IsSubclassOf(myClassType)) & vbCrLf
   End Sub
End Class
using System;

public class Class1 { }
public class DerivedC1 : Class1 { }

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Class1 myClass = new Class1();
      DerivedC1 myDClass = new DerivedC1();
      Type myClassType = myClass.GetType();
      Type myDClassType = myDClass.GetType();

      // Returns true:
      outputBlock.Text += String.Format("myDClass subclass of myClass: {0}", myDClassType.IsSubclassOf(myClassType)) + "\n";
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference