You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
3.7 KiB
C#
129 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Reflection;
|
|
|
|
namespace CCompressorXN_HelperLib
|
|
{
|
|
/// <summary>
|
|
/// 枚举操作类
|
|
/// </summary>
|
|
public class EnumHelper
|
|
{
|
|
/// <summary>
|
|
/// 根据值获取枚举方法
|
|
/// </summary>
|
|
/// <param name="enumType">typeof(枚举)</param>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public static Enum GetEnumByValue(Type enumType, int value)
|
|
{
|
|
return Enum.Parse(enumType, Enum.GetName(enumType, value)) as Enum;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取枚举的Description
|
|
/// </summary>
|
|
/// <param name="enumValue"></param>
|
|
/// <returns></returns>
|
|
public static string GetEnumDescription(Enum enumValue)
|
|
{
|
|
string value = enumValue.ToString();
|
|
FieldInfo field = enumValue.GetType().GetField(value);
|
|
object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false); //获取描述属性
|
|
if (objs == null || objs.Length == 0) //当描述属性没有时,直接返回名称
|
|
return value;
|
|
DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
|
|
return descriptionAttribute.Description;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 字符串转Enum
|
|
/// </summary>
|
|
/// <typeparam name="T">枚举</typeparam>
|
|
/// <param name="str">字符串</param>
|
|
/// <returns>转换的枚举</returns>
|
|
public static T ToEnum<T>(string str)
|
|
{
|
|
return (T)Enum.Parse(typeof(T), str);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 枚举转集合
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static List<EnumItem> GetEnumItems<T>()
|
|
{
|
|
var result = new List<EnumItem>();
|
|
|
|
Type enumType = typeof(T);
|
|
|
|
if (!enumType.IsEnum)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
string[] fieldstrs = Enum.GetNames(enumType);
|
|
|
|
foreach (var item in fieldstrs)
|
|
{
|
|
var field = enumType.GetField(item);
|
|
object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组
|
|
string description;
|
|
if (arr != null && arr.Length > 0)
|
|
{
|
|
description = ((DescriptionAttribute)arr[0]).Description; //属性描述
|
|
}
|
|
else
|
|
{
|
|
description = item; //描述不存在取字段名称
|
|
}
|
|
result.Add(new EnumItem
|
|
{
|
|
Code = item,
|
|
Value = (int)Enum.Parse(enumType, item),
|
|
Descprtion = description,
|
|
|
|
});
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据字符串获取枚举值
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="name"></param>
|
|
/// <returns></returns>
|
|
public static int ConvertEnumToInt<T>(string name)
|
|
{
|
|
try
|
|
{
|
|
return (int)Enum.Parse(typeof(T), name);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
throw;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public class EnumItem
|
|
{
|
|
/// <summary>
|
|
/// 枚举值
|
|
/// </summary>
|
|
public string Code { get; set; }
|
|
|
|
public int Value { get; set; }
|
|
|
|
/// <summary>
|
|
/// 描述
|
|
/// </summary>
|
|
public string Descprtion { get; set; }
|
|
}
|
|
}
|