Type.IsAbstract Właściwość

Definicja

Pobiera wartość wskazującą, czy Type element jest abstrakcyjny i musi zostać zastąpiony.

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

Wartość właściwości

truejeśli element jest abstrakcyjnyType; w przeciwnym razie . false

Implementuje

Przykłady

Poniższy przykład tworzy tablicę Type obiektów, które reprezentują następujące typy:zawiera wyrażenie zwracane true , jeśli określony obiekt to abstract; w przeciwnym razie zwraca wartość false.

  • AbstractClass, klasa abstrakcyjna (klasa oznaczona jako abstract w języku C# i MustInherit Visual Basic).

  • DerivedClass, klasa dziedziczona z klasy AbstractClass.

  • SingleClass, klasa nie dziedziczona. Jest ona zdefiniowana w sealed języku C# i NotInheritable w Visual Basic.

  • ITypeInfo, interfejs.

  • ImplementingClass, klasa, która implementuje ITypeInfo interfejs.

Metoda zwraca true tylko dla AbstractClassklasy , klasy abstrakcyjnej i ITypeInfointerfejsu .

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

Uwagi

Właściwość IsAbstract zwraca true następujące przypadki:

  • Bieżący typ jest abstrakcyjny; oznacza to, że nie można utworzyć wystąpienia, ale może służyć tylko jako klasa bazowa dla klas pochodnych. W języku C# klasy abstrakcyjne są oznaczone abstrakcyjnym słowem kluczowym; w języku F# są one oznaczone atrybutem AbstractClass ; w Visual Basic są one oznaczone słowem kluczowym MustInherit .

  • Bieżący typ jest interfejsem.

Jeśli bieżący Type reprezentuje parametr typu w definicji typu ogólnego lub metody ogólnej, ta właściwość zawsze zwraca wartość false.

Dotyczy

Zobacz też