(精华)2020年6月26日 C#类库 枚举类型转为选项列表帮助类
【摘要】
using System;
using System.Collections.Generic;
using System.Linq;
namespace Core.Util
{
public s...
using System;
using System.Collections.Generic;
using System.Linq;
namespace Core.Util
{
public static class EnumHelper
{
/// <summary>
/// 将枚举类型转为选项列表
/// 注:value为值,text为显示内容
/// </summary>
/// <param name="enumType">枚举类型</param>
/// <returns></returns>
public static List<SelectOption> ToOptionList(Type enumType)
{
var values = Enum.GetValues(enumType);
List<SelectOption> list = new List<SelectOption>();
foreach (var aValue in values)
{
list.Add(new SelectOption
{
value = ((int)aValue).ToString(),
text = aValue.ToString()
});
}
return list;
}
/// <summary>
/// 多选枚举转为对应文本,逗号隔开
/// </summary>
/// <param name="values">多个值</param>
/// <param name="enumType">枚举类型</param>
/// <returns></returns>
public static string ToMultipleText(List<int> values, Type enumType)
{
if (values == null)
return string.Empty;
List<string> textList = new List<string>();
var allValues = Enum.GetValues(enumType);
foreach (var aValue in allValues)
{
if (values.Contains((int)aValue))
textList.Add(aValue.ToString());
}
return string.Join(",", textList);
}
/// <summary>
/// 多选枚举转为对应文本,逗号隔开
/// </summary>
/// <param name="values">多个值逗号隔开</param>
/// <param name="enumType">枚举类型</param>
/// <returns></returns>
public static string ToMultipleText(string values, Type enumType)
{
return ToMultipleText(values?.Split(',')?.Select(x => x.ToInt())?.ToList(), enumType);
}
}
public class SelectOption
{
public string value { get; set; }
public string text { get; set; }
}
}
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/106967585
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)