int获取枚举

/// <summary>
        /// int获取枚举
        /// </summary>
        /// <typeparam name="T">枚举集合</typeparam>
        /// <param name="num"></param>
        /// <returns></returns>
        public static T NumToEnum<T>(int num)
        {
            foreach (var myCode in Enum.GetValues(typeof(T)))
            {
                if ((int)myCode == num)
                {
                    return (T)myCode;
                }
            }
            throw new ArgumentException(string.Format("{0} 未能找到对应的枚举.", num), "Description");
        }

 例子

int cardtype = Common.ObjectToInt(_context.Request["cardtype"]);
            CardVolume.CardTypeEnum cardtypeenum = Common.NumToEnum<CardVolume.CardTypeEnum>(cardtype);    

 

posted @ 2016-05-30 17:45  鞍山老菜鸟  阅读(249)  评论(0编辑  收藏  举报