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傳回,否則會false傳回。The 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,抽象類別(在 Visual Basic abstract C# MustInherit中標記為的類別)。AbstractClass, an abstract class (a class marked as abstract in C# and MustInherit in Visual Basic).

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

  • SingleClass,不可繼承的類別。SingleClass, a non-inheritable class. 它定義為sealed在C#和NotInheritable中的 Visual 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

備註

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

  • 目前的類型是 abstract;也就是說,它無法具現化,但只能做為衍生類別的基類。The current type is abstract; that is, it cannot be instantiated, but can only serve as the base class for derived classes. 在C#中,抽象類別會以abstract關鍵字標記;在 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的表示泛型型別或泛型方法定義中的類型參數,則這個屬性false一律會傳回。If the current Type represents a type parameter in the definition of a generic type or generic method, this property always returns false.

適用於

另請參閱