Type.IsEnum Type.IsEnum Type.IsEnum Type.IsEnum Property

定義

取得值,指出目前的 Type 是否表示列舉類型。Gets a value indicating whether the current Type represents an enumeration.

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

屬性值

如果目前 true 代表列舉,則為 Type,否則為 falsetrue if the current Type represents an enumeration; otherwise, false.

實作

範例

下列範例示範如何使用IsEnum屬性。The following example demonstrates how to use the IsEnum property.

using namespace System;
enum class Color
{ Red, Blue, Green };

int main()
{
   Type^ colorType = Color::typeid;
   Type^ enumType = Enum::typeid;
   Console::WriteLine( "Is Color an enum? {0}.", colorType->IsEnum );
   Console::WriteLine( "Is Color a value type? {0}.", colorType->IsValueType );
   Console::WriteLine( "Is Enum an enum Type? {0}.", enumType->IsEnum );
   Console::WriteLine( "Is Enum a value type? {0}.", enumType->IsValueType );
}
// The example displays the following output:
//     Is Color an enum? True.
//     Is Color a value type? True.
//     Is Enum an enum type? False.
//     Is Enum a value type? False.
using System;
public enum Color 
{ Red, Blue, Green }

class TestIsEnum 
{
    public static void Main() 
    {
       Type colorType = typeof(Color);
       Type enumType = typeof(Enum);
       Console.WriteLine("Is Color an enum? {0}.", colorType.IsEnum);
       Console.WriteLine("Is Color a value type? {0}.", colorType.IsValueType);
       Console.WriteLine("Is Enum an enum Type? {0}.", enumType.IsEnum);
       Console.WriteLine("Is Enum a value type? {0}.", enumType.IsValueType);
   }
}
// The example displays the following output:
//     Is Color an enum? True.
//     Is Color a value type? True.
//     Is Enum an enum type? False.
//     Is Enum a value type? False.
Imports System

Public Enum Color
    Red
    Blue
    Green
End Enum

Class Example
   Public Shared Sub Main()
        Dim colorType As Type = GetType(Color)
        Dim enumType As Type = GetType([Enum])
        Console.WriteLine("Is Color an enum? {0}.", colorType.IsEnum)
        Console.WriteLine("Is Color a value type? {0}.", colorType.IsValueType)
        Console.WriteLine("Is Enum an enum type? {0}.", enumType.IsEnum)
        Console.WriteLine("Is Enum a value type? {0}.", enumType.IsValueType)
    End Sub 
End Class
' The example displays the following output:
'     Is Color an enum? True.
'     Is Color a value type? True.
'     Is Enum an enum type? False.
'     Is Enum a value type? False.

備註

這個屬性會傳回true列舉型別,但不是適合用於Enum型別。This property returns true for an enumeration, but not for the Enum type itself.

如果目前Type代表建構泛型型別,這個屬性會套用至泛型型別定義從中建構的類型。If the current Type represents a constructed generic type, this property applies to the generic type definition from which the type was constructed. 例如,如果目前Type代表MyGenericType<int>(MyGenericType(Of Integer)在 Visual Basic 中),這個屬性的值取決於MyGenericType<T>For example, if the current Type represents MyGenericType<int> (MyGenericType(Of Integer) in Visual Basic), the value of this property is determined by MyGenericType<T>.

如果目前Type表示的類型參數的泛型類型或泛型方法定義中這個屬性一律傳回falseIf the current Type represents a type parameter in the definition of a generic type or generic method, this property always returns false.

這個屬性是唯讀的。This property is read-only.

適用於

另請參閱