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 戴雁冰扩展 /// /// 变量名转成枚举值 /// /// 枚举类型 /// 名称(数值开头时会在前面加上下划线"_") /// 默认值 /// 忽略大小写 /// public static TEnum _ToEnumValue(this string Name, TEnum DefValue, bool IgnoreCase = true) where TEnum : struct { TEnum enm; return System.Enum.TryParse(_HasHeaderNumber.IsMatch(Name) ? "_" + Name : Name, IgnoreCase, out enm) ? enm : DefValue; } static Regex _HasHeaderNumber = new Regex(@"^\d"); #endregion } }