服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
18 小时以前 a960900364d19bbf0ad7923a57989609e7fce798
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
 
namespace Tiger.Model.Extensions
{
    /// <summary>
    /// 枚举扩展方法
    /// </summary>
    internal static class EnumExtension
    {
        /// <summary>
        /// 获得枚举的名称
        /// </summary>
        /// <returns>枚举的名称</returns>
        public static string GetName(this Enum obj)
        {
            return Enum.GetName(obj.GetType(), obj);
        }
 
        /// <summary>
        /// 获得枚举的值
        /// </summary>
        /// <returns>枚举的值</returns>
        public static int GetValue(this Enum obj)
        {
            return Convert.ToInt32(obj);
        }
 
        /// <summary>
        /// 获得枚举的描述,如果没有定义描述则使用枚举的名称代替
        /// </summary>
        /// <returns>枚举的描述</returns>
        public static string GetDesc(this Enum obj)
        {
            string name = Enum.GetName(obj.GetType(), obj);
            DescriptionAttribute attribute = obj.GetType().GetField(name).GetCustomAttribute(typeof(DescriptionAttribute), true) as DescriptionAttribute;
            return attribute?.Description ?? name;
        }
 
 
        /// <summary>
        /// 根据名称或者描述获取枚举对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="description"></param>
        /// <returns></returns>
        public static T GetEnum<T>(this string description) where T : struct
        {
            Type type = typeof(T);
            foreach (var field in type.GetFields())
            {
                if (field.Name == description)
                {
                    return (T)field.GetValue(null);
                }
 
                var attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (attributes != null && attributes.FirstOrDefault() != null)
                {
                    if (attributes.First().Description == description)
                    {
                        return (T)field.GetValue(null);
                    }
                }
            }
 
            throw new ArgumentException($"{type.Name}中未能找到名称或者描述[{description}]对应的枚举.", "Description");
        }
 
        /// <summary>
        /// 根据值获取枚举对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T GetEnum<T>(this int value) where T : struct
        {
            Type type = typeof(T);
            if (type.IsEnum)
            {
                return (T)Enum.Parse(type, value.ToString());
            }
 
            throw new TypeLoadException($"{type.Name}不是枚举类型");
        }
 
        /// <summary>
        /// 根据值获取枚举描述
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetEnumDesc<T>(this int value) where T : struct
        {
            Type type = typeof(T);
            if (type.IsEnum)
            {
                return GetDesc((Enum)Enum.Parse(type, value.ToString()));
            }
 
            throw new TypeLoadException($"{type.Name}不是枚举类型");
        }
 
        /// <summary>
        /// 获取枚举内容列表
        /// </summary>
        /// <param name="type">枚举类型typeof(T)</param>
        /// <returns>返回枚举内容列表</returns>
        public static List<EnumContent> GetEnumContent(this Type type)
        {
            List<EnumContent> result = new List<EnumContent>();
            FieldInfo[] fields = type.GetFields();
            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsEnum)
                {
                    object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    result.Add(new EnumContent(field.Name, Convert.ToInt32(field.GetValue(null)), attr.Length == 0 ? field.Name : ((DescriptionAttribute)attr[0]).Description));
                }
            }
 
            return result;
        }
    }
 
    /// <summary>
    /// 枚举内容实体
    /// </summary>
    internal readonly struct EnumContent
    {
        /// <summary>
        /// 枚举内容实体
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="value">值</param>
        /// <param name="desc">描述</param>
        public EnumContent(string name, int value, string desc)
        {
            Name = name;
            Value = value;
            Desc = desc;
        }
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; }
        /// <summary>
        /// 值
        /// </summary>
        public int Value { get; }
        /// <summary>
        /// 描述
        /// </summary>
        public string Desc { get; }
    }
}