Type.IsEnum 屬性

定義

取得值,指出目前的 Type 是否表示列舉類型。

C#
public virtual bool IsEnum { get; }
C#
public bool IsEnum { get; }

屬性值

Boolean

如果目前 true 代表列舉,則為 Type,否則為 false

實作

範例

下列範例示範如何使用 IsEnum 屬性。

C#
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.

備註

這個屬性 true 會傳回列舉的,但不會傳回 Enum 型別本身。

如果目前的 Type 代表結構化泛型型別,這個屬性會套用至用來建立型別的泛型型別定義。 例如,如果目前的 Type (代表 MyGenericType<int> MyGenericType(Of Integer) Visual Basic) 中的,則這個屬性的值是由決定 MyGenericType<T>

如果目前 Type 表示泛型型別或泛型方法定義中的類型參數,則這個屬性一律會傳回 false

這是唯讀的屬性。

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

另請參閱