纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

111 lines
3.2 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
namespace ICSSoft.Common
{
public static class EnumHelper
{
#region 获取枚举注解
/// <summary>
/// 判单枚举中是否包含该枚举描述
/// </summary>
/// <param name="enumType"></param>
/// <param name="desc"></param>
/// <returns></returns>
public static bool HasDescriptions(Type enumType,string desc)
{
//if (enumType != null && enumType.IsEnum)
//{
// FieldInfo[] fields = enumType.GetFields();
// for (int i = 1; i < fields.Length; ++i)
// {
// //object fieldValue = Enum.Parse(enumType, fields[i].Name);
// object[] attrs = fields[i].GetCustomAttributes(true);
// foreach (object attr in attrs)
// {
// if (typeof(DescriptionAttribute).IsAssignableFrom(attr.GetType()) && ((DescriptionAttribute)attr).Description.Equals(desc))
// {
// return true;
// }
// }
// }
//}
if (NewTransType.list.Contains(desc))
{
return true;
}
return false;
}
#endregion
public static string GetDescription<Attr>(this Enum em) where Attr : AttrDescription
{
Type enumType = em.GetType();
var name = Enum.GetName(enumType, em);
if (name == null)
return string.Empty;
object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(Attr), false);
if (objs == null || objs.Length == 0)
return string.Empty;
Attr attr = objs[0] as Attr;
return attr.Description;
}
public static string GetDescription(this Enum em)
{
Type enumType = em.GetType();
var name = Enum.GetName(enumType, em);
if (name == null)
return string.Empty;
object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
if (objs == null || objs.Length == 0)
return string.Empty;
System.ComponentModel.DescriptionAttribute attr = objs[0] as System.ComponentModel.DescriptionAttribute;
return attr.Description;
}
}
#region 用于枚举的特性类
public class AttrDescription : Attribute
{
private string _showName;
public AttrDescription(string desc)
{
_showName = desc;
}
public string Description
{
get
{
return _showName;
}
}
}
public class DBValue : AttrDescription
{
public DBValue(string desc)
: base(desc)
{
}
}
//public class DescriptionEn : AttrDescription
//{
// public DescriptionEn(string desc)
// : base(desc)
// {
// }
//}
#endregion
}