可移植类库中的Type.IsEnum属性

Kar*_*lok 17 c# enums .net-4.6 asp.net-core

我正在尝试Portable Class Library使用ASP.NET Core 1.0以下指令编写代码:

public static void WriteMessage<T>(T value)
{
    if (typeof(T).IsEnum)
    {
        Debug.Print("Is enum")
    }
    else
    {
        Debug.Print("Not Is enum")
    }
}
Run Code Online (Sandbox Code Playgroud)

但是这段代码没有编译,因为编译器说IsEnumType上没有属性.

有什么建议?

Gab*_*gut 42

从某些功能Type被转移到TypeInfo在.NET的核心.

typeof(T).GetTypeInfo().IsEnum
Run Code Online (Sandbox Code Playgroud)

  • `GetTypeInfo()`是`IntrospectionExtensions`的扩展方法.将`using System.Reflection`添加到您的类中. (4认同)