【问题标题】:Type.IsEnum Property in Portable Class Library可移植类库中的 Type.IsEnum 属性
【发布时间】:2016-07-12 11:18:04
【问题描述】:

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

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

但是这段代码无法编译,因为编译器说属性IsEnum 在 Type 上不存在。

有什么建议吗?

【问题讨论】:

  • documentation 说:“如果当前 Type 表示泛型类型或泛型方法定义中的类型参数,则此属性始终返回 false。” this 也可能是相关的

标签: c# enums asp.net-core .net-4.6


【解决方案1】:

Type 中的一些功能已移至 .NET Core 中的 TypeInfo

typeof(T).GetTypeInfo().IsEnum

【讨论】:

  • 嗨,Gabriel,我尝试使用您的代码,但 Type 上不存在 GetTypeInfo() 函数。怎么了?
  • GetTypeInfo()IntrospectionExtensions 的扩展方法。将using System.Reflection 添加到您的班级。
  • 没错!!!我忘记了“使用”。谢谢加布里埃尔!!现在它可以工作了:-)
  • 这也对我有用。 @Karlok 记住将其标记为答案,以便它可以浮到顶部。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
  • 1970-01-01
  • 2012-08-28
  • 1970-01-01
相关资源
最近更新 更多