Type.IsAbstract Type.IsAbstract Type.IsAbstract Type.IsAbstract Property

定義

取得值,指出 Type 是否為抽象並且必須被覆寫。Gets a value indicating whether the Type is abstract and must be overridden.

public:
 property bool IsAbstract { bool get(); };
public bool IsAbstract { get; }
member this.IsAbstract : bool
Public ReadOnly Property IsAbstract As Boolean

屬性值

如果 Type 是抽象,則為 true,否則為 falsetrue if the Type is abstract; otherwise, false.

實作

範例

下列範例會建立的陣列Type代表下列類型的物件: 包含型別傳回true指定的物件是否abstract; 否則它會傳回falseThe following example creates an array of Type objects that represent the following types:contains type returns true if the specified object is abstract; otherwise, it returns false.

  • AbstractClass抽象類別 (標示為類別abstract在 C# 和MustInheritVisual Basic 中)。AbstractClass, an abstract class (a class marked as abstract in C# and MustInherit in Visual Basic).

  • DerivedClass繼承自類別AbstractClassDerivedClass, a class that inherits from AbstractClass.

  • SingleClass不可繼承的類別。SingleClass, a non-inheritable class. 它指sealed在 C# 和NotInheritableVisual Basic 中。It is defined as sealed in C# and NotInheritable in Visual Basic.

  • ITypeInfo介面。ITypeInfo, an interface.

  • ImplementingClass實作的類別ITypeInfo介面。ImplementingClass, a class that implements the ITypeInfo interface.

此方法會傳回true僅適用於AbstractClass,抽象類別,以及ITypeInfo,介面。The method returns true only for AbstractClass, the abstract class, and ITypeInfo, the interface.

using System;

public abstract class AbstractClass
{}

public class DerivedClass : AbstractClass
{}

public sealed class SingleClass
{}

public interface ITypeInfo
{
   string GetName();
}

public class ImplementingClass : ITypeInfo
{
   public string GetName()
   {
      return this.GetType().FullName;
   }
}

delegate string InputOutput(string inp);

public class Example
{
   public static void Main()
   {
      Type[] types= { typeof(AbstractClass),
                      typeof(DerivedClass),
                      typeof(ITypeInfo),
                      typeof(SingleClass),
                      typeof(ImplementingClass),
                      typeof(InputOutput) };
      foreach (var type in types)
         Console.WriteLine("{0} is abstract: {1}",
                           type.Name, type.IsAbstract);

   }
}
// The example displays the following output:
//       AbstractClass is abstract: True
//       DerivedClass is abstract: False
//       ITypeInfo is abstract: True
//       SingleClass is abstract: False
//       ImplementingClass is abstract: False
//       InputOutput is abstract: False
Public MustInherit Class AbstractClass
End Class

Public Class DerivedClass : Inherits AbstractClass
End Class

Public NotInheritable Class SingleClass
End Class

Public Interface ITypeInfo
   Function GetName() As String
End Interface

Public Class ImplementingClass : Implements ITypeInfo
   Public Function GetName() As String _
          Implements ITypeInfo.GetName
      Return Me.GetType().FullName
   End Function
End Class

Delegate Function InputOutput(inp As String) As String

Module Example
   Public Sub Main()
      Dim types() As Type = { GetType(AbstractClass),
                              GetType(DerivedClass),
                              GetType(ITypeInfo),
                              GetType(SingleClass),
                              GetType(ImplementingClass),
                              GetType(InputOutput) }
      For Each type In types
         Console.WriteLine("{0} is abstract: {1}",
                           type.Name, type.IsAbstract)
      Next
   End Sub
End Module
' The example displays the following output:
'       AbstractClass is abstract: True
'       DerivedClass is abstract: False
'       ITypeInfo is abstract: True
'       SingleClass is abstract: False
'       ImplementingClass is abstract: False
'       InputOutput is abstract: False

備註

IsAbstract屬性會傳回true在下列情況:The IsAbstract property returns true in the following cases:

  • 目前的類型是抽象的;也就是它無法具現化,但只可以做為衍生類別的基底類別。The current type is abstract; that is, it cannot be instantiated, but can only serve as the base class for derived classes. 在 C# 中,抽象類別會標示抽象關鍵字,在 Visual Basic 中,所以會標示MustInherit關鍵字。In C#, abstract classes are marked with the abstract keyword; in Visual Basic, they are marked with the MustInherit keyword.

  • 目前的類型是介面。The current type is an interface.

如果目前Type表示的類型參數的泛型類型或泛型方法定義中這個屬性一律傳回falseIf the current Type represents a type parameter in the definition of a generic type or generic method, this property always returns false.

適用於

另請參閱