事情是这样的,我个人需要将枚举转成list集合,所以写了一个方法去转换但是我想动态去接收枚举
#region 枚举List类型
public static System.Collections.Generic.List<object> GetEnumList<T>()
{
System.Collections.Generic.List<object> list = new System.Collections.Generic.List<object>();
foreach (var item in RecipeEnum.GetValues(typeof(T)))
{
list.Add(new { id = item, text = RecipeEnum.GetName(typeof(T), item) });
}
return list;
}
#endregion
想实现string动态去转换枚举名,然后通过string转换的枚举名去访问枚举
string strValue = Enume.ToString();
return Common.GetEnumList<strValue>();
以下的的方法挺繁琐的,希望能在这寻找到帮助,非常感谢!
if (!string.IsNullOrEmpty(enCode))
{
switch (enCode)
{
case "DounloadFormat":
return Content(Common.GetEnumList<DounloadFormat>().ToJson());
case "ConnectionType":
return Content(Common.GetEnumList<ConnectionType>().ToJson());
case "RecipeType":
return Content(Common.GetEnumList<RecipeEnum>().ToJson());
}
}