Type.IsAbstract 属性

定义

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

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

属性值

如果 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接口的 。

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
[<AbstractClass>]
type AbstractClass() = class end

type DerivedClass() =  inherit AbstractClass()

[<Sealed>]
type SingleClass() = class end

type ITypeInfo =
   abstract GetName: unit -> string

type ImplementingClass() =
    interface ITypeInfo with
        member this.GetName() =
            this.GetType().FullName

type DiscriminatedUnion = 
    | Yes 
    | No of string

type Record = 
  { Name: string
    Age: int }

type InputOutput = delegate of inp: string -> string

let types = 
    [ typeof<AbstractClass>
      typeof<DerivedClass>
      typeof<ITypeInfo>
      typeof<SingleClass>
      typeof<ImplementingClass>
      typeof<DiscriminatedUnion>
      typeof<Record>
      typeof<InputOutput> ]
for typ in types do
    printfn $"{typ.Name} is abstract: {typ.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
//       DiscriminatedUnion is abstract: True
//       Record 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

  • 当前类型是抽象的;也就是说,它不能实例化,但只能用作派生类的基类。 在 C# 中,抽象类使用抽象关键字 (keyword) 标记;在 F# 中,它们使用 AbstractClass 属性进行标记;在 Visual Basic 中,它们使用 MustInherit 关键字 (keyword) 标记。

  • 当前类型是接口。

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此属性始终返回 false

适用于

另请参阅