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,是不可繼承的類別。 它定義為 sealed C# 和 NotInheritable Visual Basic 中的 。

  • ITypeInfo,介面。

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

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

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# 中,抽象類會以 abstract 關鍵詞標示;在 F# 中,它們會以 AbstractClass 屬性標示;在 Visual Basic 中,它們會以 MustInherit 關鍵詞標示。

  • 目前的類型是介面。

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

適用於

另請參閱