Type.IsAbstract 属性

定义

获取一个值,通过该值指示 Type 是否为抽象的并且必须被重写。

C#
public bool IsAbstract { get; }

属性值

如果 Type 是抽象的,则为 true;否则为 false

实现

示例

以下示例创建表示以下类型的对象的数组Type:如果指定的对象为 abstract,则包含类型返回 true ;否则返回 false

  • AbstractClass,抽象类 (在 C# 和 MustInherit Visual Basic) 中标记为 abstract 的类。

  • DerivedClass,一个继承自 AbstractClass的类。

  • SingleClass,不可继承的类。 它在 C# 和 NotInheritable Visual Basic 中定义为 sealed

  • ITypeInfo,接口。

  • ImplementingClass,实现 接口的 ITypeInfo 类。

方法 true 仅返回抽象 AbstractClass类 和 ITypeInfo接口的 。

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# 中,抽象类使用抽象关键字 (keyword) 标记;在 F# 中,它们使用 AbstractClass 属性进行标记;在 Visual Basic 中,它们使用 MustInherit 关键字 (keyword) 标记。

  • 当前类型是接口。

如果当前 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

另请参阅