using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ssystem.Public.Common.Util
{
/// <summary>
/// 枚举对象工具类
/// </summary>
[Serializable]
public class EnumHelper
{
/// <summary>
/// 通过枚举值获取枚举类型
/// </summary>
/// <typeparam name="T">枚举</typeparam>
/// <param name="value">值</param>
/// <returns>枚举值,如果不存在则返回第一个枚举值</returns>
public static T GetEnumByValue<T>(string value)
{
Type type = typeof(T);
if (value == null)
value = "";
var item = from enumItem in type.GetEnumNames()
where enumItem.ToLower() == value.ToLower()
select enumItem;
if (item.Count() > 0)
{
return (T)System.Enum.Parse(type, item.ElementAt(0), true);
}
else
return (T)System.Enum.Parse(type, type.GetEnumNames()[0], true);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ssystem.Public.Common.Util
{
/// <summary>
/// 枚举对象工具类
/// </summary>
[Serializable]
public class EnumHelper
{
/// <summary>
/// 通过枚举值获取枚举类型
/// </summary>
/// <typeparam name="T">枚举</typeparam>
/// <param name="value">值</param>
/// <returns>枚举值,如果不存在则返回第一个枚举值</returns>
public static T GetEnumByValue<T>(string value)
{
Type type = typeof(T);
if (value == null)
value = "";
var item = from enumItem in type.GetEnumNames()
where enumItem.ToLower() == value.ToLower()
select enumItem;
if (item.Count() > 0)
{
return (T)System.Enum.Parse(type, item.ElementAt(0), true);
}
else
return (T)System.Enum.Parse(type, type.GetEnumNames()[0], true);
}
}
}