EnumExtensions.cs 4.42 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Newtonsoft.Json;

namespace Hh.Mes.Common
{
    public static class EnumExtensions
    {

        /// <summary>
        /// 转换int
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static int GetInt(Enum enumType)
        {
            return Convert.ToInt32(enumType);
        }


        /// <summary>
        /// 转换josn
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static string GetJsonEnum(this  Type enumType)
        {
            return GetJsonEnum(enumType, null);
        }

        /// <summary>
        /// 返回格式{"初始化":"10"}
        /// </summary>
        public static string GetJsonEnum(Type enumType, string alias)
        {
            int[] values = (int[])Enum.GetValues(enumType);
            string[] names = Enum.GetNames(enumType);
            string[] pairs = new string[values.Length];

            for (int i = 0; i < values.Length; i++)
            {
                pairs[i] = $"\"{names[i]}\":{ values[i]}";
            }

            if (string.IsNullOrEmpty(alias))
                alias = enumType.Name;

            return string.Format("var {0}={{\n{1}\n}}", alias, string.Join(",\n", pairs));
        }


        public static string GetJsonEnums(this Type enumType)
        {
            return GetJsonEnums(enumType, null);
        }

        /// <summary>
        /// 返回格式 {"10":"初始化"}
        /// </summary>
        public static string GetJsonEnums(Type enumType, string alias)
        {
            int[] values = (int[])Enum.GetValues(enumType);
            string[] names = Enum.GetNames(enumType);
            string[] pairs = new string[values.Length];

            for (int i = 0; i < values.Length; i++)
            {
                pairs[i] = $"\"{values[i]}\":  \"{ names[i] }\"";
            }

            if (string.IsNullOrEmpty(alias))
                alias = enumType.Name;

            return string.Format("var {0}={{\n{1}\n}}", alias, string.Join(",\n", pairs));
        }

        #region 枚举 
        /// <summary>
        /// 获得枚举字段的特性(Attribute),该属性不允许多次定义。
        /// </summary>
        public static string GetAttributeValue(this Enum thisValue)
        {
            FieldInfo field = thisValue.GetType().GetField(thisValue.ToString());
            var attr = (Attribute.GetCustomAttribute(field, typeof(Desc)) as Desc);
            if (attr == null) return string.Empty;
            return (Attribute.GetCustomAttribute(field, typeof(Desc)) as Desc)?.Value;
        }

        /// <summary>
        /// 获得枚举字段的特性(Attribute),该属性不允许多次定义。
        /// </summary>
        public static T GetAttribute<T>(this Enum thisValue) where T : class
        {
            FieldInfo field = thisValue.GetType().GetField(thisValue.ToString());
            var attr = (Attribute.GetCustomAttribute(field, typeof(T)) as T);
            return attr;
        }

        /// <summary>
        /// 获得枚举字段的名称。
        /// </summary>
        /// <returns></returns>
        public static string GetName(this Enum thisValue)
        {
            return Enum.GetName(thisValue.GetType(), thisValue);
        }

        /// <summary>
        /// 获得枚举字段的值。
        /// </summary>
        /// <returns></returns>
        public static T GetValue<T>(this Enum thisValue)
        {
            return (T)Enum.Parse(thisValue.GetType(), thisValue.ToString());
        } 
        #endregion

    }

    /// <summary>
    /// 字段或属性的中文解释属性 使用:[Desc("描述")]
    /// </summary>
    [Serializable]
    [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
    public class Desc : Attribute
    {
        /// <summary>
        /// 获得字段或属性的中文解释.
        /// </summary>
        /// <value>字段或属性的中文解释.</value>
        public string Value { get; private set; }

        /// <summary>
        /// 初始化创建一个 <see cref="Desc"/> 类的实例, 用于指示字段或属性的解释说明.
        /// </summary>
        /// <param name="value">字段或属性的解释说明.</param>
        public Desc(string value)
        {
            Value = value;
        }
    }
}