Type.IsAbstract プロパティ

定義

Type が抽象型で、オーバーライドする必要があるかどうかを示す値を取得します。

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

プロパティ値

true が抽象である場合は Type。それ以外の場合は false

実装

次の例では、次の型を表す オブジェクトの Type 配列を作成します。指定したオブジェクトが の場合は contains 型が返 true されます。それ以外の場合は abstractを返します false

  • AbstractClass、抽象クラス (C# および MustInherit Visual Basic で としてabstractマークされたクラス)。

  • DerivedClassから AbstractClass継承するクラス。

  • SingleClass、継承不可能なクラス。 これは、C# と NotInheritable Visual Basic で とsealed定義されています。

  • ITypeInfo、インターフェイス。

  • ImplementingClassインターフェイスを実装する ITypeInfo クラス。

メソッドは、 、抽象クラス、および ITypeInfoインターフェイスに対AbstractClassしてのみ を返trueします。

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

適用対象

こちらもご覧ください