c# 枚举帮助类

ublic class EnumHelper

{

#region 返回枚举对应属性

/// <summary>

/// 返回枚举对应属性

/// </summary>

/// <typeparam name="T">枚举类</typeparam>

/// <returns></returns>

public static List<Model.EnumType> EnumToList<T>()

{

List<Model.EnumType> list = new List<Model.EnumType>();

var items = Enum.GetValues(typeof(T));

foreach (var item in items)

{

Model.EnumType entity = new Model.EnumType();

object[] obj = item.GetType().GetField(item.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);

if (obj != null && obj.Length > 0)

{

DescriptionAttribute da = obj[0] as DescriptionAttribute;

entity.Desction = da.Description;

}

entity.Value = Convert.ToInt32(item);

entity.Name = item.ToString();

list.Add(entity);

}

return list;

}

#endregion

#region 获取枚举类型描述

/// <summary>

/// 获取枚举类型描述

/// </summary>

/// <param name="enumValue">枚举值</param>

/// <returns></returns>

public static string GetEnumDescription(Enum enumValue)

{

string str = enumValue.ToString();

System.Reflection.FieldInfo field = enumValue.GetType().GetField(str);

object[] objs = field.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);

if (objs == null || objs.Length == 0) return str;

System.ComponentModel.DescriptionAttribute da = (System.ComponentModel.DescriptionAttribute)objs[0];

return da.Description;

}

#endregion

#region 字符串转枚举

/// <summary>

/// 字符串转枚举

/// </summary>

/// <typeparam name="T">枚举类型</typeparam>

/// <param name="value">名称</param>

/// <returns></returns>

public static T StringToEnum<T>(string value)

{

var obj = (T)Enum.Parse(typeof(T), value, false);

return obj;

}

#endregion

}

相关推荐
TTGGGFF3 分钟前
Supertonic 部署与使用全流程保姆级指南(附已部署镜像)
开发语言·python
木木木一7 分钟前
Rust学习记录--C7 Package, Crate, Module
开发语言·学习·rust
love530love7 分钟前
升级到 ComfyUI Desktop v0.7.0 版本后启动日志报 KeyError: ‘tensorrt‘ 错误解决方案
开发语言·windows·python·pycharm·virtualenv·comfyui·comfyui desktop
Evand J1 小时前
【MATLAB例程】【空地协同】UAV辅助的UGV协同定位,无人机辅助地面无人车定位,带滤波,附MATLAB代码下载链接
开发语言·matlab·无人机·无人车·uav·协同定位·ugv
chao1898441 小时前
基于MATLAB实现多变量高斯过程回归(GPR)
开发语言·matlab·回归
ytttr8736 小时前
隐马尔可夫模型(HMM)MATLAB实现范例
开发语言·算法·matlab
天远Date Lab6 小时前
Python实战:对接天远数据手机号码归属地API,实现精准用户分群与本地化运营
大数据·开发语言·python
listhi5207 小时前
基于Gabor纹理特征与K-means聚类的图像分割(Matlab实现)
开发语言·matlab
野生的码农7 小时前
码农的妇产科实习记录
android·java·人工智能
qq_433776427 小时前
【无标题】
开发语言·php