using System;
using System. Collections. Generic;
using System. ComponentModel;
using System. Reflection;
namespace Lenovo. HIS. Common
{
public static class EnumDefinitionExt
{
#region GetDescription
public static string GetDescription ( this Enum value , Boolean nameInstead = true )
{
Type type = value . GetType ( ) ;
string name = Enum. GetName ( type, value ) ;
if ( string . IsNullOrEmpty ( name) )
{
return string . Empty;
}
FieldInfo field = type. GetField ( name) ;
DescriptionAttribute attribute = Attribute. GetCustomAttribute ( field, typeof ( DescriptionAttribute) ) as DescriptionAttribute;
if ( attribute == null && nameInstead == true )
{
return name;
}
return attribute == null ? null : attribute. Description;
}
public static string GetDescription ( Type enumType, object val)
{
string name = System. Enum. GetName ( enumType, val) ;
if ( string . IsNullOrEmpty ( name) )
{
return string . Empty;
}
FieldInfo field = enumType. GetField ( name) ;
object [ ] attrArray = field. GetCustomAttributes ( typeof ( DescriptionAttribute) , true ) ;
if ( attrArray. Length > 0 )
{
DescriptionAttribute desc = attrArray[ 0 ] as DescriptionAttribute;
if ( desc != null )
{
return desc. Description;
}
}
return name;
}
#endregion
#region GetEnumList
public static List< EnumNVDTrinity> GetEnumList < T > ( )
{
List< EnumNVDTrinity> list = new List < EnumNVDTrinity > ( ) ;
EnumNVDTrinity trinity = null ;
Type type = typeof ( T) ;
foreach ( int itemVal in Enum. GetValues ( typeof ( T) ) )
{
trinity = new EnumNVDTrinity ( ) ;
trinity. Name = Enum. GetName ( type, itemVal) ;
trinity. Value = itemVal;
trinity. Description = GetDescription ( type, itemVal) ;
list. Add ( trinity) ;
}
return list;
}
#endregion
#region GetModel
public static T GetModel < T > ( int value )
{
T myEnum = ( T) Enum. Parse ( typeof ( T) , value . ToString ( ) , true ) ;
return myEnum;
}
public static T GetModel < T > ( string value )
{
T myEnum = ( T) Enum. Parse ( typeof ( T) , value , true ) ;
return myEnum;
}
#endregion
}
}
using System;
using System. Collections. Generic;
using System. Linq;
using System. Text;
namespace Lenovo. HIS. Common
{
public class EnumNVDTrinity
{
public string Name { get ; set ; }
public int Value { get ; set ; }
public string Description { get ; set ; }
#region 重写方法 - ToString
public override string ToString ( )
{
return Value. ToString ( ) ;
}
#endregion
}
}