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實值型別 (適用於結構和列舉型別) 即使 boxing 處理。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.

注意

這個屬性會傳回trueforType執行個體,代表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.

適用於

另請參閱