dotnetcore GetTypeInfo()没有在object类型的变量上定义?

Pau*_*aul 2 c# .net-core

下面的代码是原始代码的精简版本,用于演示此问题.在dotnetcore(1.0.1)中.IsEnum属性被移动到System.Reflection.我做了一些改变,一切按预期工作.但是,我无法工作的是类型对象.

编译器抛出此错误:c:\ tmp \netcore\repro\Program.cs(14,17):错误CS0103:当前上下文中不存在名称"t"

public class Program
{
    enum Kleur {Red, Blue, Green}

    public static void Main(string[] args)
    {
        object myType = Kleur.Green;
        if (myType.GetTypeInfo().IsEnum)
        {
            Console.WriteLine("Yes its an enum");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

是否有一种解决方法来测试对象是否是dotnetcore中的Enum类型?是否有一个特定的原因,为什么没有类型对象的扩展方法(我需要的所有其他类型似乎工作).

svi*_*ick 6

当从Type那里搬到时TypeInfo,更换.GetType()不仅仅是.GetTypeInfo(),它是.GetType().GetTypeInfo().那是因为Type没有删除,而是拆分成了Type,只包含了一些基本成员,TypeInfo剩下的就是其中之一.

所以,你的代码应该是:

object myType = Kleur.Green;
if (myType.GetType().GetTypeInfo().IsEnum)
{
    Console.WriteLine("Yes its an enum");
}
Run Code Online (Sandbox Code Playgroud)