Type.IsClass 属性

定义

获取一个值,通过该值指示 Type 是否是一个类或委托;即,不是值类型或接口。Gets a value indicating whether the Type is a class or a delegate; that is, not a value type or interface.

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

属性值

如果 Type 是类,则为 true;否则为 falsetrue if the Type is a class; otherwise, false.

实现

示例

下面的示例创建一个类型的实例,并指示该类型是否为类。The following example creates an instance of a type and indicates whether the type is a class.

using namespace System;
using namespace System::Reflection;
public ref class MyDemoClass{};

int main()
{
   try
   {
      Type^ myType = Type::GetType( "MyDemoClass" );
      
      // Get and display the 'IsClass' property of the 'MyDemoClass' instance.
      Console::WriteLine( "\nIs the specified type a class? {0}.", myType->IsClass );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "\nAn exception occurred: {0}.", e->Message );
   }

}

using System;
using System.Reflection;

public  class MyDemoClass
{
}

public class MyTypeClass
{
    public static void Main(string[] args)
    {
        try
        {
            Type  myType = typeof(MyDemoClass);
            // Get and display the 'IsClass' property of the 'MyDemoClass' instance.
            Console.WriteLine("\nIs the specified type a class? {0}.", myType.IsClass); 
        }
        catch(Exception e)
        {
            Console.WriteLine("\nAn exception occurred: {0}." ,e.Message);
        }
    }
}
Imports System.Reflection

Public Class MyDemoClass
End Class

Public Class MyTypeClass
    Public Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyDemoClass)
            ' Get and display the 'IsClass' property of the 'MyDemoClass' instance.
            Console.WriteLine(ControlChars.Cr + "Is the specified type a class? {0}.", myType.IsClass.ToString())
        Catch e As Exception
            Console.WriteLine(ControlChars.Cr + "An exception occurred: {0}.", e.Message.ToString())
        End Try
    End Sub
End Class

注解

此属性返回类和委托 trueThis property returns true for classes as well as delegates. 它会返回值类型的 false (对于结构和枚举),即使它们已装箱也是如此。It returns false for value types (for structures and enumerations) even if they are boxed.

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此属性始终返回 true。如果当前 Type 表示构造泛型类型,则此属性将在泛型类型定义为类定义时返回 true;也就是说,它不定义接口或值类型。If the current Type represents a type parameter in the definition of a generic type or generic method, this property always returns true.If the current Type represents a constructed generic type, this property returns true if the generic type definition is a class definition; that is, it does not define an interface or a value type.

备注

此属性为表示 EnumValueTypeType 实例返回 trueThis property returns true for Type instances that represent the Enum and ValueType classes. 这两个类分别是枚举和值类型的基类型,但它们不是枚举或值类型本身。These two classes are the base types for enumerations and value types, respectively, but they are not enumerations or value types themselves. 有关详细信息,请参阅 "IsValueType" 和 "IsEnum" 属性。For more information, see the IsValueType and IsEnum properties.

TypeAttributes.ClassSemanticsMask 的枚举值将类型声明分为类或接口。但是,类和值类型均标记有 TypeAttributes.Class 特性。The TypeAttributes.ClassSemanticsMask enumeration value distinguishes a type declaration as class or interface.However, both classes and value types are marked with the TypeAttributes.Class attribute. 如果检索类型属性的值,并使用 TypeAttributes.ClassSemanticsMask 值来确定类型是否是类而不是值类型,则还必须调用 IsValueType 属性。If you retrieve the value of a type's Attributes property and use the TypeAttributes.ClassSemanticsMask value to determine whether a type is a class instead of a value type, you must also call the IsValueType property. TypeAttributes 枚举的示例包含附加信息以及 anexample。The example for the TypeAttributes enumeration contains additional information as well as anexample.

此属性是只读的。This property is read-only.

适用于

另请参阅