一、C#获取特性(坑)

  1. [Description("系统状态码")]
  2. [Display(Name = "成功", Description = "请求成功!")]
  3. Success = 0,
  1. [Description("系统状态码")]
  2. [Display(Name = "成功", Description = "请求错误!")]
  3. err= 0,
  4.  
  5.        
  1. 当你
  1. var s=SatausCode.CommonCodeEnum.err.GetDisplayDescription();  //获取err特性 但是因为都是0,那么就匹配第一个特性的,而不是根据我err 来的, 因为 我很多的时候就会使用到200
  2.  
  3. 各位

--

  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.ComponentModel.DataAnnotations;
  6. using System.Reflection;
  7.  
  8. namespace Test
  9. {
  10.  
  11. class Program
  12. {
  13.  
  14. static void Main(string[] args)
  15. {
  16. var s=SatausCode.CommonCodeEnum.GetReqCreditDatalOK.GetDisplayDescription();
  17. }
  18.  
  19. }
  20.  
  21. public static class SatausCode
  22. {
  23. public enum CommonCodeEnum
  24. {
  25. /// <summary>
  26. /// 成功
  27. /// </summary>
  28. [Description("系统状态码")]
  29. [Display(Name = "成功", Description = "请求成功!")]
  30. Success = ,
  31.  
  32. /// <summary>
  33. /// 系统状态码
  34. /// </summary>
  35. [Description("系统状态码")]
  36. [Display(Name = "失败", Description = "请求失败!")]
  37. Error = ,
  38.  
  39. /// <summary>
  40. /// 验签状态码
  41. /// </summary>
  42. [Description("验签状态码")]
  43. [Display(Name = "部分参数不完整", Description = "部分参数不完整!")]
  44. Incomplete = ,
  45.  
  46. /// <summary>
  47. /// 系统状态码
  48. /// </summary>
  49. [Description("系统状态码")]
  50. [Display(Name = "内部服务异常", Description = "内部服务异常!")]
  51. Ex = ,
  52.  
  53. /// <summary>
  54. /// 系统状态码
  55. /// </summary>
  56. [Description("验签状态码")]
  57. [Display(Name = "timeStamp参数异常", Description = "timeStamp参数异常!")]
  58. TimeStamp_Str = ,
  59.  
  60. /// <summary>
  61. /// 系统状态码
  62. /// </summary>
  63. [Description("验签状态码")]
  64. [Display(Name = "请求超时", Description = "请求超时!")]
  65. Timeout = ,
  66.  
  67. /// <summary>
  68. /// 系统状态码
  69. /// </summary>
  70. [Description("验签状态码")]
  71. [Display(Name = "用户权限限制", Description = "用户权限限制!")]
  72. Power = ,
  73.  
  74. /// <summary>
  75. /// 系统状态码
  76. /// </summary>
  77. [Description("验签状态码")]
  78. [Display(Name = "签名不合法", Description = "签名不合法!")]
  79. Sign = ,
  80.  
  81. /// <summary>
  82. /// 系统状态码
  83. /// </summary>
  84. [Description("区分城市状态码")]
  85. [Display(Name = "未授权此城市", Description = "未授权此城市!")]
  86. DisArea = ,
  87.  
  88. /// <summary>
  89. /// 系统状态码
  90. /// </summary>
  91. [Description("系统状态码")]
  92. [Display(Name = "远程服务器返回错误: (404) 未找到。", Description = "远程服务器返回错误: (404) 未找到!")]
  93. NoFind = ,
  94.  
  95. /// <summary>
  96. /// 系统状态码
  97. /// </summary>
  98. [Description("系统状态码")]
  99. [Display(Name = "远程服务器返回错误: (500) 内部服务器错误。", Description = "远程服务器返回错误: (500) 内部服务器错误。")]
  100. NoErr = ,
  101.  
  102. #region 房估估
  103. /// <summary>
  104. /// 房估估
  105. /// </summary>
  106. [Description("第三方")]
  107. [Display(Name = "查询小区/地址不能为空", Description = "查询小区/地址不能为空!")]
  108. VillageNull = ,
  109.  
  110. /// <summary>
  111. /// 房估估
  112. /// </summary>
  113. [Description("房估估第三方")]
  114. [Display(Name = "小区匹配", Description = "小区数据获取成功!")]
  115. GetVillageOk = ,
  116.  
  117. /// <summary>
  118. /// 房估估 ------------报错信息 用它的 暂不使用
  119. /// </summary>
  120. [Description("房估估第三方")]
  121. [Display(Name = "小区匹配", Description = "小区获取数据失败!")]
  122. GetVillageErr = ,
  123.  
  124. /// <summary>
  125. /// 房估估
  126. /// </summary>
  127. [Description("房估估第三方")]
  128. [Display(Name = "房估估小区评价", Description = "小区评价数据成功!")]
  129. GetHouseEvalOk = ,
  130.  
  131. /// <summary>
  132. /// 房估估
  133. /// </summary>
  134. [Description("房估估第三方")]
  135. [Display(Name = "房估估小区评价", Description = "小区评价数据成功!")]
  136. GetHouseEvalErr = ,
  137. #endregion
  138.  
  139. #region 同盾
  140.  
  141. /// <summary>
  142. ///
  143. /// </summary>
  144. [Description("征信第三方")]
  145. [Display(Name = "请求凭证无效", Description = "请求凭证无效!")]
  146. CertErr = ,
  147.  
  148. /// <summary>
  149. /// 征信
  150. /// </summary>
  151. [Description("征信第三方")]
  152. [Display(Name = "请求用户数据", Description = "请求用户数据成功!")]
  153. GetReqCreditDatalOK = ,
  154.  
  155. /// <summary>
  156. /// 征信
  157. /// </summary>
  158. [Description("征信第三方")]
  159. [Display(Name = "请求用户数据", Description = "请求用户数据失败!")]
  160. GetReqCreditDatalErr = ,
  161.  
  162. /// <summary>
  163. /// 征信
  164. /// </summary>
  165. [Description("征信第三方")]
  166. [Display(Name = "获取用户数据", Description = "获取用户数据成功!")]
  167. GetCreditDataOK = ,
  168.  
  169. /// <summary>
  170. /// 征信
  171. /// </summary>
  172. [Description("征信第三方")]
  173. [Display(Name = "获取用户数据", Description = "获取用户数据失败!")]
  174. GetCreditDataErr = ,
  175.  
  176. /// <summary>
  177. /// 征信
  178. /// </summary>
  179. [Description("征信第三方")]
  180. [Display(Name = "获取详情成功", Description = "获取详情成功!")]
  181. GetCreditDetailOK = ,
  182.  
  183. /// <summary>
  184. /// 征信
  185. /// </summary>
  186. [Description("征信第三方")]
  187. [Display(Name = "小区地址有误", Description = "小区地址有误!")]
  188. Getvillage = ,
  189.  
  190. /// <summary>
  191. /// 征信
  192. /// </summary>
  193. [Description("征信第三方")]
  194. [Display(Name = "获取数据详情", Description = "获取数据详情失败!")]
  195. GetCreditDetailErr = ,
  196.  
  197. /// <summary>
  198. /// 征信
  199. /// </summary>
  200. [Description("征信第三方")]
  201. [Display(Name = "请求数据", Description = "请求数据详情成功!")]
  202. GetReqCreditDetailOK = ,
  203.  
  204. /// <summary>
  205. /// 征信
  206. /// </summary>
  207. [Description("征信第三方")]
  208. [Display(Name = "请求数据", Description = "请求数据详情失败!")]
  209. GetReqCreditDetailErr = ,
  210.  
  211. #endregion
  212.  
  213. #region 房估估验证
  214. /// <summary>
  215. /// 系统状态码
  216. /// </summary>
  217. [Description("房估估验证")]
  218. [Display(Name = "输入房产面积数据格式有误", Description = "输入房产面积数据格式有误!")]
  219. Areaformat = ,
  220. /// <summary>
  221. /// 系统状态码
  222. /// </summary>
  223. [Description("房估估验证")]
  224. [Display(Name = "输入层数据格式有误", Description = "输入层数据格式有误!")]
  225. Floorformat = ,
  226.  
  227. /// <summary>
  228. /// 系统状态码
  229. /// </summary>
  230. [Description("房估估验证")]
  231. [Display(Name = "输入总楼层数据格式有误", Description = "输入总楼层数据格式有误!")]
  232. TotalFloorformat = ,
  233.  
  234. /// <summary>
  235. /// 系统状态码
  236. /// </summary>
  237. [Description("房估估验证")]
  238. [Display(Name = "所在楼层大于总楼层数据有误", Description = "所在楼层大于总楼层数据有误!")]
  239. TotalBigFloorformat = ,
  240.  
  241. #endregion
  242.  
  243. #region 同盾验证
  244. /// <summary>
  245. /// 系统状态码
  246. /// </summary>
  247. [Description("同盾验证")]
  248. [Display(Name = "身份证号格式不对", Description = "身份证号格式不对!")]
  249. CardIdformat = ,
  250.  
  251. /// <summary>
  252. /// 系统状态码
  253. /// </summary>
  254. [Description("同盾验证")]
  255. [Display(Name = "手机号格式不对", Description = "手机号格式不对!")]
  256. Telformat = ,
  257.  
  258. #endregion
  259.  
  260. /// <summary>
  261. /// 系统状态码
  262. /// </summary>
  263. [Description("评估")]
  264. [Display(Name = "评估失败", Description = "评估失败!")]
  265. EvelErr = ,
  266. }
  267.  
  268. /// <summary>
  269. /// 获取特性 (DisplayAttribute) 的说明;如果未使用该特性,则返回枚举的名称。
  270. /// </summary>
  271. /// <param name="enumValue"></param>
  272. /// <returns></returns>
  273. public static string GetDisplayDescription(this Enum enumValue)
  274. {
  275. FieldInfo fieldInfo = enumValue.GetType().GetField(enumValue.ToString());
  276. DisplayAttribute[] attrs =
  277. fieldInfo.GetCustomAttributes(typeof(DisplayAttribute), false) as DisplayAttribute[];
  278.  
  279. return attrs.Length > ? attrs[].Description : enumValue.ToString();
  280. }
  281.  
  282. }
  283.  
  284. }

二、解决方案

1、状态码(模块化区分)

  1. /// <summary>
  2. /// 上传状态
  3. /// </summary>
  4. public enum UploadState
  5. {
  6. [Description("上传失败")]
  7. UploadFailed = -,
  8.  
  9. [Description("正在上传")]
  10. Uploading = ,
  11.  
  12. [Description("上传成功")]
  13. UploadSuccess =
  14. }

方法

  1. /// <summary>
  2. /// 获取枚举的描述
  3. /// </summary>
  4. /// <param name="en">枚举</param>
  5. /// <returns>返回枚举的描述</returns>
  6. public static string GetDescription(Enum en)
  7. {
  8. Type type = en.GetType(); //获取类型
  9. MemberInfo[] memberInfos = type.GetMember(en.ToString()); //获取成员
  10. if (memberInfos!=null&&memberInfos.Length>)
  11. {
  12. DescriptionAttribute[] attrs = memberInfos[].GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; //获取描述特性
  13.  
  14. if (attrs!=null&&attrs.Length>)
  15. {
  16. return attrs[].Description; //返回当前描述
  17. }
  18. }
  19. return en.ToString();
  20. }

调用代码

  1. EnumService.GetDescription(UploadState.UploadSuccess)

2、指定名称获取特性描述

一、C#获取特性(坑)的更多相关文章

  1. C# 使用表达式树获取特性的值

    一.定义特性 /// <summary> /// 定义特性 /// </summary> [AttributeUsage(AttributeTargets.Field | At ...

  2. C# 通过反射获取特性的值

    特性(Attribute)是用于在运行时传递程序中各种元素(比如类.方法.结构.枚举.组件等)的行为信息的声明性标签.您可以通过使用特性向程序添加声明性信息.一个声明性标签是通过放置在它所应用的元素前 ...

  3. c#获取特性DescriptionAttribute的值

    int detailId = Convert.ToInt32(id); BillLoanApplyDetail model = _billLoadApplyDetail.GetBillLoanAppl ...

  4. MVC中获取模型属性的Range和StringLength验证特性设置

    MVC中的客户端及服务端模型验证信息都以ModelMetadata类型作为承载,在获得属性的ModelMetadata之后(还不知道怎么获取ModelMetadata的童鞋请自行恶补),我们可以轻松得 ...

  5. .NET(C#):使用反射来获取枚举的名称、值和特性【转】

    首先需要从内部了解一下枚举(Enumeration),相信许多人已经知道了,当我们声明一个这样的枚举类型: enumMyEnum { AAA, BBB, CCC } 背后的IL是这样的: .class ...

  6. .NET(C#):使用反射来获取枚举的名称、值和特性

    首先需要从内部了解一下枚举(Enumeration),相信许多人已经知道了,当我们声明一个这样的枚举类型: enum MyEnum { AAA, BBB, CCC } 背后的IL是这样的: .clas ...

  7. JavaScript特性(attribute)、属性(property)和样式(style)

    最近在研读一本巨著<JavaScript忍者秘籍>,里面有一篇文章提到了这3个概念. 书中的源码可以在此下载.我将源码放到了线上,如果不想下载,可以直接访问在线网址,修改页面名就能访问到相 ...

  8. [C#] C# 知识回顾 - 特性 Attribute

    C# 知识回顾 - 特性 Attribute [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5911289.html 目录 特性简介 使用特性 特性 ...

  9. 深入学习jQuery特性操作

    × 目录 [1]获取特性 [2]设置特性 [3]删除特性 前面的话 每个元素都有一个或者多个特性,这些特性的用途就是给出相应元素或者其内容的附加信息.操作特性的DOM方法主要有3个:getAttrib ...

随机推荐

  1. 解决安装mysql-connector-odbc-5.3.2 错误1918……不能加载安装或转换器库……的BUG

    还是在虚拟机Windows Server 2003上安装mysql-connector-odbc-5.3.2,装着装着就报错了,大致是“错误1918……不能加载安装或转换器库……”,问我Retry,I ...

  2. 共享OrCAD9.2pSpice9.2+multisim下载地址

    http://pan.baidu.com/s/1dDcfiH7ewb9绿色版,解压后即可用!http://pan.baidu.com/s/1kTG43WFMultisim v11绿色版.7zhttp: ...

  3. 032:DTL常用过滤器(1)

    为什么需要过滤器: 在DTL中,不支持函数的调用形式‘()’,因此不能给函数传递参数,这将有很大的局限性:而过滤器其实就是一个函数,可以对需要处理的参数进行处理,并且还可以额外接受一个参数(也就是说: ...

  4. Vue项目【饿了么App】mock数据【data.json】

    1.前后端分离式开发,约定好数据字段接口! 2.前端mock静态数据,开发完毕后,与后端进行数据联调! 3.vue.config.js 配置 devServer const appData = req ...

  5. redis测试

    1,安装redis软件 2,引入redis jar包 3,案例 package test; import java.util.List; import redis.clients.jedis.Jedi ...

  6. hibernate插入数据测试无异常,但数据库没有数据

    解决方法: spring test测试默认会将事务回滚,如果想阻止spring transactional回滚,在test方法上加注解@Rollback(false)即可. Hibernate hql ...

  7. MapGIS IGServer for java

    但是安装完之后,服务里面没有找到igserver服务 IGServer SManager cxf 怎么会找不到类呢?是服务映射出了问题,所以目录找不到.所以怎么配置目录呢?是在xml还是环境变量,还是 ...

  8. 在成为测试大牛的路上,我推荐BestTest

    BestTest-Python自动化测试9月份班开始招生啦! 网络+现场同步进行,课程新升级,web自动化+接口自动化双管齐下,一线互联网测试开发工程师带你在自动化的世界里自由翱翔! 推荐优惠多多,欢 ...

  9. HihoCoder - 1104 Suzhou Adventure (树上背包)

    题目:https://vjudge.net/contest/323605#problem/D 题意:给你一棵n个点的树,1点出发,然后规定k个点必须去,每个点上有一个权值,要走m个点,问最大权值是多少 ...

  10. Apache检查配置文件语法

    Windows环境:httpd -t或者: httpd.exe -w -t -f "C:\Apache2.2\conf\httpd.conf" -d "C:\Apache ...