Type.IsClass Type.IsClass Type.IsClass Type.IsClass Property

定义

获取一个值,通过该值指示 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
Imports System.Reflection
Imports Microsoft.VisualBasic

Public Class MyDemoClass
End Class 'MyDemoClass

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 'Main
End Class 'MyTypeClass

注解

此属性返回true为委托或类。This 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.

备注

此属性返回true有关Type实例,分别代表EnumValueType类。This 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. 有关详细信息,请参阅IsValueTypeIsEnum属性。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.

适用于

另请参阅