Type.IsAbstract Vlastnost

Definice

Získá hodnotu označující, zda Type je abstraktní a musí být přepsána.

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

Hodnota vlastnosti

trueType pokud je abstraktní, jinak hodnota false.

Implementuje

Příklady

Následující příklad vytvoří pole Type objektů, které představují následující typy:contains type vrátí true , pokud je abstractzadaný objekt ; v opačném případě vrátí false.

  • AbstractClass, abstraktní třída (třída označená jako abstract v jazyce C# a MustInherit v jazyce Visual Basic).

  • DerivedClass, třída, která dědí z AbstractClass.

  • SingleClass, neděditelná třída. je definovaný jako sealed v jazyce C# a NotInheritable v jazyce Visual Basic.

  • ITypeInfo, rozhraní.

  • ImplementingClass, třída, která implementuje ITypeInfo rozhraní .

Metoda vrací true pouze pro AbstractClass, abstraktní třídu a ITypeInfo, rozhraní .

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

Poznámky

Vlastnost IsAbstract se vrátí true v následujících případech:

  • Aktuální typ je abstraktní; to znamená, že nelze vytvořit instanci, ale může sloužit pouze jako základní třída pro odvozené třídy. V jazyce C# jsou abstraktní třídy označeny klíčovým slovem abstract ; V jazyce F# jsou označeny atributem AbstractClass ; v jazyce Visual Basic jsou označeny klíčovým slovem MustInherit .

  • Aktuálním typem je rozhraní.

Pokud current Type představuje parametr typu v definici obecného typu nebo obecné metody, tato vlastnost vždy vrátí false.

Platí pro

Viz také