Type.IsClass 屬性

定義

取得值,表示 Type 是類別或委派,也就是非實值類型或介面。

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

屬性值

如果 Type 是類別,則為 true,否則為 false

實作

範例

下列範例會建立類型的實例,並指出類型是否為類別。

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);
        }
    }
}
type MyDemoClass = class end

try
    let myType = typeof<MyDemoClass>
    // Get and display the 'IsClass' property of the 'MyDemoClass' instance.
    printfn $"\nIs the specified type a class? {myType.IsClass}."
with e ->
    printfn $"\nAn exception occurred: {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 針對類別和委派傳回 。 它會針對結構與列舉 (實值型別傳回 false) 即使已Boxed也一樣。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,這個屬性一律會傳 true回 。 如果目前的 Type 代表建構的泛型型別,則如果泛型型別定義是類別定義,則此屬性會傳回 true ;也就是說,它不會定義介面或實值型別。

注意

這個屬性會 true 針對 Type 表示 EnumValueType 類別的實體傳回 。 這兩個類別分別是列舉型別和實值型別的基底型別,但它們不是列舉型別或實值型別本身。 如需詳細資訊,請參閱 IsValueTypeIsEnum 屬性。

列舉 TypeAttributes.ClassSemanticsMask 值會將類型宣告區分為類別或介面。 不過,類別和實值型別都會以 TypeAttributes.Class 屬性標示。 如果您擷取類型 Attributes 屬性的值,並使用 TypeAttributes.ClassSemanticsMask 值來判斷類型是否為類別,而不是實值型別,您也必須呼叫 IsValueType 屬性。 列舉的 TypeAttributes 範例包含其他資訊和example。

這是唯讀的屬性。

適用於

另請參閱