wyb
2021-05-11 49ce087bd2a34a150597e1cc1da157af242c0b6d
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
 
namespace Pcb.Common.Extension
{
    public static class EnumExtension
    {
        public static string GetNote(this System.Enum value)
        {
            var field = value.GetType().GetField(value.ToString());
            var attributes = (NoteAttribute[])field.GetCustomAttributes(typeof(NoteAttribute), false);
            if (attributes.Length == 0)
            {
                return string.Empty;
            }
            else
            {
                return attributes[0].Note;
            }
 
        }
        #region 戴雁冰扩展
        /// <summary>
        /// 变量名转成枚举值
        /// </summary>
        /// <typeparam name="TEnum">枚举类型</typeparam>
        /// <param name="Name">名称(数值开头时会在前面加上下划线"_")</param>
        /// <param name="DefValue">默认值</param>
        /// <param name="IgnoreCase">忽略大小写</param>
        /// <returns>值</returns>
        public static TEnum _ToEnumValue<TEnum>(this string Name, TEnum DefValue, bool IgnoreCase = true) where TEnum : struct
        {
            TEnum enm;
            return System.Enum.TryParse<TEnum>(_HasHeaderNumber.IsMatch(Name) ? "_" + Name : Name, IgnoreCase, out enm) ? enm : DefValue;
        }
        static Regex _HasHeaderNumber = new Regex(@"^\d");
        #endregion
    }
}