EnumExtensions.cs 3.66 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>
        /// 转换json 返回格式{"初始化":"10"}
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static string GetJsonEnum(this Type enumType, string alias = null)
        {
            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 $"var {alias}={{\n{string.Join(",\n", pairs)}\n}};";
        }


        #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;
        }
    }

    /// <summary>
    /// 自动输出到前端页面 枚举特性
    /// </summary>
    [AttributeUsage(AttributeTargets.Enum)]
    public class SysPublicEnumAttribute : Attribute
    {

    }
}