C# dotnetcore GetTypeInfo()未在object类型的变量上定义?
下面的代码是演示问题的原始代码的精简版本。在dotnetcore(1.0.1)中,IsEnum属性被移动到System.Reflection。我做了一些改变,所有的工作都如期进行。但是,我无法使其工作的类型是object 编译器抛出此错误: c:\tmp\netcore\repro\Program.cs(14,17):错误CS0103:名称“t”在当前上下文中不存在C# dotnetcore GetTypeInfo()未在object类型的变量上定义?,c#,.net-core,C#,.net Core,下面的代码是演示问题的原始代码的精简版本。在dotnetcore(1.0.1)中,IsEnum属性被移动到System.Reflection。我做了一些改变,所有的工作都如期进行。但是,我无法使其工作的类型是object 编译器抛出此错误: c:\tmp\netcore\repro\Program.cs(14,17):错误CS0103:名称“t”在当前上下文中不存在 public class Program { enum Kleur {Red, Blue, Green} pub
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");
}
}
}
在dotnetcore中是否有测试对象是否为Enum类型的变通方法?类型对象没有扩展方法的具体原因是什么(我需要的所有其他类型似乎都能工作)。当从
类型
移动到类型信息
时,对.GetType()
的替换不仅仅是.GetTypeInfo()
,而是.GetType().GetTypeInfo()
。这是因为没有删除Type
,而是将其分为Type
(只包含一些基本成员)和TypeInfo
(包含其余成员)
因此,您的代码应该是:
object myType = Kleur.Green;
if (myType.GetType().GetTypeInfo().IsEnum)
{
Console.WriteLine("Yes its an enum");
}
当从
Type
移动到TypeInfo
时,.GetType()
的替换对象不仅仅是.GetTypeInfo()
,而是.GetType().GetTypeInfo()
。这是因为没有删除Type
,而是将其分为Type
(只包含一些基本成员)和TypeInfo
(包含其余成员)
因此,您的代码应该是:
object myType = Kleur.Green;
if (myType.GetType().GetTypeInfo().IsEnum)
{
Console.WriteLine("Yes its an enum");
}
myType.GetType().IsEnum适用于我(因为它是对象)。是的,在.Net核心控制台应用程序中。myType.GetType().IsEnum适用于我(因为它的对象)。是的,在.Net核心控制台应用程序中