string转Enum名称进行访问

事情是这样的,我个人需要将枚举转成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());
                }
            }
        public static T EnumValueOf<T>(int index)
        {
            return (T)Enum.Parse(typeof(T), Enum.GetNames(typeof(T)).GetValue(index).ToString());
        }

        public static Hashtable EnumToHashtable<T>()
        {
            Hashtable ht = new Hashtable();
            for (int i = 0; i < EnumLength<T>(); i++)
            {
                T v = EnumValueOf<T>(i);
                ht[Enum.GetName(typeof(T), v)] = (int)Enum.GetValues(typeof(T)).GetValue(i);
            }
            return ht;
        }

我这里有个枚举泛型转 hashtable的,你可以自己转成list

1.避免这样干。使用枚举就是为了避免string这种弱类型多了空格少了空格写错个字符导致匹配不上,才使用enum强类型来作为判断的依据,你用了string为什么还要用enum,放弃enum不好吗
2.如果你就是非要动态访问,可以string转int,enum本质上就是一组int,你直接赋值对应的数字也行的

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632