我有一个带有Enum的类,我需要在运行时使用反射加载该类,并将字符串值与枚举进行比较,并使用Enum.TryParse评估它是否为有效的枚举。任何想法谢谢
答案 0 :(得分:2)
这个怎么样?
// converting enums to strings is easy
String WhatDayItIs = DayOfWeek.Monday.ToString();
// converting strings to enums is a bit more work
DayOfWeek WhatDayItIsDOW;
if (Enum.IsDefined(typeof(DayOfWeek), WhatDayItIs))
WhatDayItIsDOW = (DayOfWeek)Enum.Parse(typeof(DayOfWeek),
答案 1 :(得分:1)
会这样吗?
public class myClass
{
public enum myEnum
{
Item1,
Item2
}
}
public class otherClass
{
public otherClass()
{
if (Enum.GetNames(typeof(myClass.myEnum)).Contains("Item1"))
Console.WriteLine("We have a match!");
}
}