Type.IsAbstract 屬性

定義

取得值,指出 Type 是否為抽象並且必須被覆寫。

C#
public bool IsAbstract { get; }

屬性值

如果 Type 是抽象,則為 true,否則為 false

實作

範例

下列範例會建立代表下列類型的 物件陣列Type:如果指定的物件為 abstract,則包含傳回true型別;否則會傳false回 。

  • AbstractClass,抽象類 (C# 和 MustInherit Visual Basic) 中標示為 abstract 的類別。

  • DerivedClass,繼承自 AbstractClass的類別。

  • SingleClass,是不可繼承的類別。 它定義為 sealed C# 和 NotInheritable Visual Basic 中的 。

  • ITypeInfo,介面。

  • ImplementingClass,實作 介面的 ITypeInfo 類別。

方法只會針對 AbstractClass、抽象類和 ,傳ITypeInfotrue 介面。

C#
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

備註

屬性 IsAbstract 會在下列情況下傳回 true

  • 目前的類型是抽象的;也就是說,它無法具現化,但只能做為衍生類別的基類。 在 C# 中,抽象類會以 abstract 關鍵詞標示;在 F# 中,它們會以 AbstractClass 屬性標示;在 Visual Basic 中,它們會以 MustInherit 關鍵詞標示。

  • 目前的類型是介面。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,這個屬性一律會傳 false回 。

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱