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

屬性值

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

備註

這個屬性會 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 表示泛型型別或泛型方法定義中的類型參數,則這個屬性一律會傳回 trueIf the current Type represents a type parameter in the definition of a generic type or generic method, this property always returns true. 如果目前的 Type 代表結構化泛型型別,則 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 表示和類別的實例傳回 Enum ValueTypeThis 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.ClassThe 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. 如果您抓取型別屬性(attribute)屬性(attribute)的值,並使用 TypeAttributes.ClassSemanticsMask 該值來判斷型別是否為類別,而不是實值型別,您也必須呼叫 IsValueType 屬性(property)。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.

適用於

另請參閱