using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Linq;
|
using System.Reflection;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace GTech.Solution.Api.Common
|
{
|
public class EnumUtil
|
{
|
public static TEnum Parse<TEnum>(int? value)
|
{
|
try
|
{
|
Type enumType = typeof(TEnum);
|
return (TEnum)Enum.Parse(enumType, value.ToString());
|
}
|
catch (Exception)
|
{
|
throw;
|
}
|
}
|
|
public static TEnum Parse<TEnum>(string str)
|
{
|
try
|
{
|
TEnum result = (TEnum)Enum.Parse(typeof(TEnum), str);
|
|
return result;
|
}
|
catch (Exception)
|
{
|
throw;
|
}
|
}
|
|
public static string GetDescription<T>(int? value)
|
{
|
var enumerationValue = Parse<T>(value);
|
if (enumerationValue == null)
|
return string.Empty;
|
|
Type type = enumerationValue.GetType();
|
MemberInfo[] memberInfo = type.GetMember(enumerationValue.ToString());
|
|
object[] attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
|
if (attrs != null && attrs.Length > 0)
|
{
|
return ((DescriptionAttribute)attrs[0]).Description;
|
}
|
else
|
{
|
return enumerationValue.ToString();
|
}
|
}
|
|
public static TDescriptionType GetDescription<TDescriptionType>(Enum enumerationValue)
|
{
|
Type type = enumerationValue.GetType();
|
MemberInfo[] memberInfo = type.GetMember(enumerationValue.ToString());
|
|
object[] attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
|
if (attrs != null && attrs.Length > 0)
|
{
|
var result = ((DescriptionAttribute)attrs[0]).Description;
|
return (TDescriptionType)Convert.ChangeType(result, typeof(TDescriptionType));
|
}
|
else
|
{
|
return (TDescriptionType)Convert.ChangeType(enumerationValue, typeof(TDescriptionType));
|
}
|
}
|
|
public static string GetEnumString<TEnum>(int? value)
|
{
|
if (!value.HasValue)
|
return string.Empty;
|
|
Type enumType = typeof(TEnum);
|
return Enum.Parse(enumType, value.ToString()).ToString();
|
}
|
}
|
}
|