Blame view

sys/Hh.Mes.Common/Enum/EnumExtensions.cs 3.66 KB
赖素文 authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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>
25
        /// 转换json 返回格式{"初始化":"10"}
赖素文 authored
26
27
28
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
29
        public static string GetJsonEnum(this Type enumType, string alias = null)
赖素文 authored
30
31
32
33
34
35
36
        {
            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++)
            {
37
                pairs[i] = $"\"{names[i]}\":{values[i]}";
赖素文 authored
38
39
40
41
42
            }

            if (string.IsNullOrEmpty(alias))
                alias = enumType.Name;
43
44
            // 返回时添加分号
            return $"var {alias}={{\n{string.Join(",\n", pairs)}\n}};";
赖素文 authored
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
        }


        #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;
        }
    }
113
114
115
116
117
118
119
120
121

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

    }
赖素文 authored
122
}