This article is a mirror article of machine translation, please click here to jump to the original article.

View: 14473|Reply: 2

[ASP.NET] Simple enumeration operations

[Copy link]
Posted on 1/10/2019 9:58:32 AM | | | |
        /// <summary>
        Get the enum name based on the value of the enum
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetEnumNameByKey<T>(int key)
        {
            string nameStr = string. Empty;
            var type = typeof(T);
            nameStr = Enum.GetName(type, key);
            return nameStr;
        }

        /// <summary>
        Get the enum name based on the value of the enum
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GetEnumNameByKey<T>(T t)
        {
            string nameStr = string. Empty;
            var type = typeof(T);
            nameStr = Enum.GetName(type, t);
            return nameStr;
        }

        /// <summary>
        Strings are converted to enum types
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        private static bool StringConvertToEnum<T>(string str, out T t) where T : new()
        {
            var obj = false;
            t = new T();
            try
            {
                t = (T)Enum.Parse(typeof(T), str);
                obj = true;
            }
            catch (Exception ex)
            {
                obj = false;
            }

            return obj;
        }

        /// <summary>
        Get a collection of enum names
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string[] GetNamesArr<T>()
        {
            return Enum.GetNames(typeof(T));
        }

        /// <summary>
        Convert enums into dictionary collections
        /// </summary>
        <typeparam name="T"> enumerate types</typeparam>
        /// <returns></returns>
        public static Dictionary<string, int> GetEnumDic<T>()
        {

            Dictionary<string, int> resultList = new Dictionary<string, int>();
            Type type = typeof(T);
            var strList = GetNamesArr<T>(). ToList();
            foreach (string key in strList)
            {
                string val = Enum.Format(type, Enum.Parse(type, key), "d");
                resultList.Add(key, int. Parse(val));
            }
            return resultList;
        }

        /// <summary>
        Get a description of the parameters
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GetDescriptionByKey<T>(T t)
        {
            var type = t.GetType();
            FieldInfo fieldInfo = type. GetField(GetEnumNameByKey<T>(t));
            DescriptionAttribute attribute = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
            var Description = attribute. Description;
            return Description;
        }







Previous:3rd Year Class A - From now on, everyone is a hostage [Japanese drama] [720P] [Updated to 01]
Next:js implementation to download files
Posted on 1/10/2019 10:10:07 AM |
Posted on 1/10/2019 10:11:33 AM |
Program Code Please insert Add code text
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com