OSS.Common获取枚举字典列表标准库支持

上篇(.Net Standard扩展支持实例分享)介绍了OSS.Common的标准库支持扩展,也列举了可能遇到问题的解决方案。由于时间有限,同时.net standard暂时还没有提供对DescriptionAttribute的支持,所以其中的转化枚举到字典列表的扩展当时按照第一种处理方式先行屏蔽,这次按照第三种方式完善一下。

  既然.net standard 下没有提供对DescriptAttribute的支持,首先我先自定义一个Attribute来补充:

    [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]      
public class OSDescriptAttribute : Attribute{
public OSDescriptAttribute(string description){
this.Description = description;}
public string Description { get; set; }}

  其次定义一个线程安全的字典,来全局缓存枚举对应的枚举字典列表,减少下次获取的代码执行:

     private static ConcurrentDictionary<string, Dictionary<string, string>> enumDirs  
=new ConcurrentDictionary<string, Dictionary<string, string>>();

  最后我们来实现获取字典部分的具体操作:

        public static Dictionary<string, string> ToEnumDirs(this Type enType, 
bool isIntValue = true){#if NETFW if (!enType.IsEnum)#elseif (!enType.GetTypeInfo().IsEnum)#endifthrow new ArgumentException("获取枚举字典,参数必须是枚举类型!"); string key = string.Concat(enType.FullName, isIntValue);Dictionary<string, string> dirs;enumDirs.TryGetValue(key, out dirs);
if (dirs != null)
return dirs.Copy();dirs = new Dictionary<string, string>();
var values = Enum.GetValues(enType);
foreach (var value in values){
var name = Enum.GetName(enType, value);
string resultValue = isIntValue ? ((int) value).ToString() : value.ToString()
;#if NETFW
var attr = enType.GetField(name)?.GetCustomAttribute<OSDescriptAttribute>();
#elsevar attr = enType.GetTypeInfo().GetDeclaredField(name)?.GetCustomAttribute<OSDescriptAttribute>();#endifdirs.Add(resultValue, attr == null ? name : attr.Description);}enumDirs.TryAdd(key, dirs);
return dirs.Copy();}

以后我们就可以在所有的业务的代码中进行  typeof(枚举类型).ToEnumDirs()  的方法来获取枚举对应的字典列表,例如:

typeof (ResultTypes).ToEnumDirs();

如有其它疑问,欢迎关注公众号(osscoder):

原文地址:http://www.cnblogs.com/sunhoy/p/6388528.html


.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/326924.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C#实现人脸识别【SqlHelper】

操作数据库工具类&#xff1a; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; using System.Configuration; using…

八皇后问题---回溯

package com.atguigu.recursion;/*** 创建人 wdl* 创建时间 2021/3/21* 描述*/ public class Queue8 {//先定义一个max表示共有多少个皇后int max8;//定义数组array&#xff0c;保存皇后防止位置的结果&#xff0c;比如 arr[8] {0 , 4, 7, 5, 2, 6, 1, 3}int[] arraynew int[m…

程序员的情人节礼物:当天微软开始Build 2017登记

2016 年 12 月&#xff0c;微软曾经宣布 Build 2017 大会将于 2017 年 5 月 10 日至 12 日在美国西雅图举行&#xff1b;时隔两个月&#xff0c;关于 Build 2017 大会的消息&#xff0c;终于有了新的进展。 2 月 10 日&#xff0c;微软在官方博客中宣布 Build 2017 大会将开启注…

jQuery的三种bind/One/Live/On事件绑定使用方法

转载自 jQuery的三种bind/One/Live/On事件绑定使用方法 jQuery是 一款优秀的JavaScript框架,在旧版里主要用bind()方法&#xff0c;在新版里又多了两种One(),Live()&#xff0c;下面介绍这几种方法的使用 本篇文章介绍了&#xff0c;关于jQuery新的事件绑定机制on()的使用技…

C#实现人脸识别【Users】

这个类主要就是model类&#xff0c;对应数据库中的表users: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Camtest {public class Users{//编号public long id { get; set; }//姓名public…

查找前端依赖 jquery css js 时间控件 不要用远程依赖 会变化的 card

BootCDN - Bootstrap 中文网开源项目免费 CDN 加速服务 找到你要的版本

使用VS Code从零开始开发调试.NET Core 1.1

使用VS Code 从零开始开发调试.NET Core 1.1。无需安装VS 2017 RC 即可开发调试.NET Core 1.1应用。 .NET Core 1.1 发布也有一段时间了&#xff0c;最大的改动是从 project.json 还原回了csproj 。 今天微软发布 .NET Core SDK 1.0 RC4 版本&#xff0c;离RTM版本也很近了。 对…

Linux 退出保存/不保存

先按ESC键 然后输入 :wq 保存并退出 :q! 不保存退出 你可以强制退出&#xff0c;或者先保存在退出 保存&#xff1a;w 强制退出 &#xff1a;q&#xff01; 无论是否退出 vi&#xff0c;均可保存所做的工作。按 ESC 键&#xff0c;确定 vi 是否处于命令模式。 操作 键入 保…

冒泡排序+推导过程

推导过程 代码实现 package com.atguigu.sort;import java.util.Arrays;/*** 创建人 wdl* 创建时间 2021/3/21* 描述*/ public class BubbleSort {public static void main(String[] args) {int arr[]{3,9,-1,10,-2};//为了容易理解&#xff0c;我们吧冒泡排序的演变过程给大家…

jQuery 基础教程 (五)之使用jQuery创建动画效果

一、jQuery 中的动画: 隐藏和显示 &#xff08;1&#xff09;hide() 在 HTML 文档中, 为一个元素调用 hide() 方法会将该元素的 display 样式改为 none. 代码功 能同 css(“display”, “none”); &#xff08;2&#xff09;show() 将元素的 display 样式改为先前的显示状 态…

人脸检测解析json的工具类face_test

这个类主要是解析json数据 using face; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Camtest {public class fac…

.Net Core 之 MSBuild 介绍

前言 关于 .NET Core 旧版本的 sdk 介绍可以参看我以前的 这篇 文章。 8 个小时前&#xff0c;.NET Core 项目组释放了 .NET Core 新一轮的 sdk 工具更新&#xff0c;即 RC4 版本 &#xff0c;这个版本也就是意味着基本功能已经确定了&#xff0c;下个版本应该就是RTM版了&…

mysql加索引快很多

3秒 变成 0.1秒 左连接的字段加索引 order by字段加索引 频繁使用的检索字段加索引

冒泡排序优化后

package com.atguigu.sort;import java.util.Arrays;/*** 创建人 wdl* 创建时间 2021/3/21* 描述*/ public class BubbleSort {public static void main(String[] args) {int arr[]{3,9,-1,10,20};//为了容易理解&#xff0c;我们吧冒泡排序的演变过程给大家展示//第一趟排序&a…

jQuery 基础教程 (四)之jQuery中的DOM操作

一、jQuery 中的 DOM 操作 &#xff08;1&#xff09;DOM(Document Object Model—文档对象模型)&#xff1a;一 种与浏览器, 平台, 语言无关的接口, 使用该接口可以 轻松地访问页面中所有的标准组件 &#xff08;2&#xff09;DOM 操作的分类: (A)DOM Core: DOM Core 并不专…

人脸检测的model类facemodel

这个类主要就是人脸检测用到的实体模型 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace face {public class facemodel{//年龄public string age { get; set; }//美丑public string beauty …

.Net大户的选择:Windows Container在携程的应用

数人云上海&深圳两地“ 容器之Mesos/K8S/Swarm三国演义”的嘉宾精彩实录第四弹&#xff01;小数已经被接连不断的干货搞晕了&#xff0c;沉浸技术的海洋好幸福~Windows container在国内的实践还比较少&#xff0c;携程作为.Net大户&#xff0c;率先进行了调研和实践应用&am…

BigDecimal保留两位小数,不足两位补0

// 四舍五入 BigDecimal value new BigDecimal(object.toString()).setScale(2,BigDecimal.ROUND_HALF_UP); // 不足两位小数补0 DecimalFormat decimalFormat new DecimalFormat("0.00#"); String strVal decimalFormat.format(value);