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.Reflection

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 Enum會傳回代表和ValueType類別的實例。TypeThis 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.

適用於

另請參閱