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 = obj0 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)objs0;

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

}

相关推荐
隐积20 小时前
3.1.7.Qt容器大家族(3):QVariant——万能盒子
开发语言·qt
都叫我大帅哥1 天前
Java JJWT 完全指南:从入门到生产级实践
java
名字还没想好☜1 天前
Next.js ‘use client‘ 到底加在哪:Server/Client Components 边界与常见报错
开发语言·前端·javascript·react·next.js
初级代码游戏1 天前
iOS开发 Swift 速记1:变量和基本数据类型
开发语言·ios·swift
前端开发张小七1 天前
Java 学习笔记 · 第一课:基础语法与核心概念
java·后端
海盗12341 天前
微软技术周报 ——2026-08-03
后端·python·microsoft·c#·.netcore
酷在前行1 天前
【R生态】PERMANOVA 进阶实战:距离选择、PERMDISP、两两比较与受限置换(保姆级教程)
开发语言·r语言
ThsPool1 天前
【ENVI二次开发学习整理 04】ENVI功能扩展与二次开发
java·前端·javascript
cxr8281 天前
Graphify vs GitNexus vs CodeGraph — 三工具架构深度对比
开发语言·架构·知识图谱
赤壁小虾1 天前
【渲染流水线】[逐片元阶段]-[透明度测试]以UnityURP为例
java·前端·数据库