Type.IsAbstract 属性

定义

获取一个值,通过该值指示 Type 是否为抽象的并且必须被重写。Gets a value indicating whether the Type is abstract and must be overridden.

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

属性值

如果 Type 是抽象的,则为 true;否则为 falsetrue if the Type is abstract; otherwise, false.

实现

示例

下面的Type示例创建对象的数组,这些对象表示以下类型:如果指定的对象为abstract,则包含类型返回true ; 否则返回falseThe following example creates an array of Type objects that represent the following types:contains type returns true if the specified object is abstract; otherwise, it returns false.

  • AbstractClass,抽象类(在abstract C#和MustInherit中标记为的类 Visual Basic)。AbstractClass, an abstract class (a class marked as abstract in C# and MustInherit in Visual Basic).

  • DerivedClass,是从AbstractClass继承的类。DerivedClass, a class that inherits from AbstractClass.

  • SingleClass,不可继承的类。SingleClass, a non-inheritable class. 它在和sealed NotInheritable Visual Basic 中C#定义为。It is defined as sealed in C# and NotInheritable in Visual Basic.

  • ITypeInfo,接口。ITypeInfo, an interface.

  • ImplementingClass,是实现ITypeInfo接口的类。ImplementingClass, a class that implements the ITypeInfo interface.

方法只为true AbstractClass、抽象类和ITypeInfo接口返回。The method returns true only for AbstractClass, the abstract class, and ITypeInfo, the interface.

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
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下,属性将返回:The IsAbstract property returns true in the following cases:

  • 当前类型是抽象的;也就是说,它不能进行实例化,但只能充当派生类的基类。The current type is abstract; that is, it cannot be instantiated, but can only serve as the base class for derived classes. 在C#中,抽象类标有abstract关键字;在 Visual Basic 中,它们用MustInherit关键字进行标记。In C#, abstract classes are marked with the abstract keyword; in Visual Basic, they are marked with the MustInherit keyword.

  • 当前类型是接口。The current type is an interface.

如果当前Type表示泛型类型或泛型方法的定义中的类型参数,则此属性始终返回falseIf the current Type represents a type parameter in the definition of a generic type or generic method, this property always returns false.

适用于

另请参阅