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