一、C#获取特性(坑)
[Description("系统状态码")]
[Display(Name = "成功", Description = "请求成功!")]
Success = 0,
[Description("系统状态码")] [Display(Name = "成功", Description = "请求错误!")] err= 0,
当你
var s=SatausCode.CommonCodeEnum.err.GetDisplayDescription(); //获取err特性 但是因为都是0,那么就匹配第一个特性的,而不是根据我err 来的, 因为 我很多的时候就会使用到200
各位
--
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Reflection; namespace Test { class Program { static void Main(string[] args) { var s=SatausCode.CommonCodeEnum.GetReqCreditDatalOK.GetDisplayDescription(); } } public static class SatausCode { public enum CommonCodeEnum { /// <summary> /// 成功 /// </summary> [Description("系统状态码")] [Display(Name = "成功", Description = "请求成功!")] Success = 0, /// <summary> /// 系统状态码 /// </summary> [Description("系统状态码")] [Display(Name = "失败", Description = "请求失败!")] Error = 1, /// <summary> /// 验签状态码 /// </summary> [Description("验签状态码")] [Display(Name = "部分参数不完整", Description = "部分参数不完整!")] Incomplete = 3, /// <summary> /// 系统状态码 /// </summary> [Description("系统状态码")] [Display(Name = "内部服务异常", Description = "内部服务异常!")] Ex = 4, /// <summary> /// 系统状态码 /// </summary> [Description("验签状态码")] [Display(Name = "timeStamp参数异常", Description = "timeStamp参数异常!")] TimeStamp_Str = 5, /// <summary> /// 系统状态码 /// </summary> [Description("验签状态码")] [Display(Name = "请求超时", Description = "请求超时!")] Timeout = 6, /// <summary> /// 系统状态码 /// </summary> [Description("验签状态码")] [Display(Name = "用户权限限制", Description = "用户权限限制!")] Power = 7, /// <summary> /// 系统状态码 /// </summary> [Description("验签状态码")] [Display(Name = "签名不合法", Description = "签名不合法!")] Sign = 8, /// <summary> /// 系统状态码 /// </summary> [Description("区分城市状态码")] [Display(Name = "未授权此城市", Description = "未授权此城市!")] DisArea = 9, /// <summary> /// 系统状态码 /// </summary> [Description("系统状态码")] [Display(Name = "远程服务器返回错误: (404) 未找到。", Description = "远程服务器返回错误: (404) 未找到!")] NoFind = 10, /// <summary> /// 系统状态码 /// </summary> [Description("系统状态码")] [Display(Name = "远程服务器返回错误: (500) 内部服务器错误。", Description = "远程服务器返回错误: (500) 内部服务器错误。")] NoErr = 11, #region 房估估 /// <summary> /// 房估估 /// </summary> [Description("第三方")] [Display(Name = "查询小区/地址不能为空", Description = "查询小区/地址不能为空!")] VillageNull = 220, /// <summary> /// 房估估 /// </summary> [Description("房估估第三方")] [Display(Name = "小区匹配", Description = "小区数据获取成功!")] GetVillageOk = 200, /// <summary> /// 房估估 ------------报错信息 用它的 暂不使用 /// </summary> [Description("房估估第三方")] [Display(Name = "小区匹配", Description = "小区获取数据失败!")] GetVillageErr = 201, /// <summary> /// 房估估 /// </summary> [Description("房估估第三方")] [Display(Name = "房估估小区评价", Description = "小区评价数据成功!")] GetHouseEvalOk = 200, /// <summary> /// 房估估 /// </summary> [Description("房估估第三方")] [Display(Name = "房估估小区评价", Description = "小区评价数据成功!")] GetHouseEvalErr = 201, #endregion #region 同盾 /// <summary> /// /// </summary> [Description("征信第三方")] [Display(Name = "请求凭证无效", Description = "请求凭证无效!")] CertErr = 228, /// <summary> /// 征信 /// </summary> [Description("征信第三方")] [Display(Name = "请求用户数据", Description = "请求用户数据成功!")] GetReqCreditDatalOK = 200, /// <summary> /// 征信 /// </summary> [Description("征信第三方")] [Display(Name = "请求用户数据", Description = "请求用户数据失败!")] GetReqCreditDatalErr = 201, /// <summary> /// 征信 /// </summary> [Description("征信第三方")] [Display(Name = "获取用户数据", Description = "获取用户数据成功!")] GetCreditDataOK = 202, /// <summary> /// 征信 /// </summary> [Description("征信第三方")] [Display(Name = "获取用户数据", Description = "获取用户数据失败!")] GetCreditDataErr = 203, /// <summary> /// 征信 /// </summary> [Description("征信第三方")] [Display(Name = "获取详情成功", Description = "获取详情成功!")] GetCreditDetailOK = 200, /// <summary> /// 征信 /// </summary> [Description("征信第三方")] [Display(Name = "小区地址有误", Description = "小区地址有误!")] Getvillage = 205, /// <summary> /// 征信 /// </summary> [Description("征信第三方")] [Display(Name = "获取数据详情", Description = "获取数据详情失败!")] GetCreditDetailErr = 205, /// <summary> /// 征信 /// </summary> [Description("征信第三方")] [Display(Name = "请求数据", Description = "请求数据详情成功!")] GetReqCreditDetailOK = 206, /// <summary> /// 征信 /// </summary> [Description("征信第三方")] [Display(Name = "请求数据", Description = "请求数据详情失败!")] GetReqCreditDetailErr = 203, #endregion #region 房估估验证 /// <summary> /// 系统状态码 /// </summary> [Description("房估估验证")] [Display(Name = "输入房产面积数据格式有误", Description = "输入房产面积数据格式有误!")] Areaformat = 301, /// <summary> /// 系统状态码 /// </summary> [Description("房估估验证")] [Display(Name = "输入层数据格式有误", Description = "输入层数据格式有误!")] Floorformat = 302, /// <summary> /// 系统状态码 /// </summary> [Description("房估估验证")] [Display(Name = "输入总楼层数据格式有误", Description = "输入总楼层数据格式有误!")] TotalFloorformat = 303, /// <summary> /// 系统状态码 /// </summary> [Description("房估估验证")] [Display(Name = "所在楼层大于总楼层数据有误", Description = "所在楼层大于总楼层数据有误!")] TotalBigFloorformat = 304, #endregion #region 同盾验证 /// <summary> /// 系统状态码 /// </summary> [Description("同盾验证")] [Display(Name = "身份证号格式不对", Description = "身份证号格式不对!")] CardIdformat = 401, /// <summary> /// 系统状态码 /// </summary> [Description("同盾验证")] [Display(Name = "手机号格式不对", Description = "手机号格式不对!")] Telformat = 402, #endregion /// <summary> /// 系统状态码 /// </summary> [Description("评估")] [Display(Name = "评估失败", Description = "评估失败!")] EvelErr = 403, } /// <summary> /// 获取特性 (DisplayAttribute) 的说明;如果未使用该特性,则返回枚举的名称。 /// </summary> /// <param name="enumValue"></param> /// <returns></returns> public static string GetDisplayDescription(this Enum enumValue) { FieldInfo fieldInfo = enumValue.GetType().GetField(enumValue.ToString()); DisplayAttribute[] attrs = fieldInfo.GetCustomAttributes(typeof(DisplayAttribute), false) as DisplayAttribute[]; return attrs.Length > 0 ? attrs[0].Description : enumValue.ToString(); } } }
二、解决方案
1、状态码(模块化区分)
/// <summary> /// 上传状态 /// </summary> public enum UploadState { [Description("上传失败")] UploadFailed = -1, [Description("正在上传")] Uploading = 0, [Description("上传成功")] UploadSuccess = 1 }
方法
/// <summary> /// 获取枚举的描述 /// </summary> /// <param name="en">枚举</param> /// <returns>返回枚举的描述</returns> public static string GetDescription(Enum en) { Type type = en.GetType(); //获取类型 MemberInfo[] memberInfos = type.GetMember(en.ToString()); //获取成员 if (memberInfos!=null&&memberInfos.Length>0) { DescriptionAttribute[] attrs = memberInfos[0].GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; //获取描述特性 if (attrs!=null&&attrs.Length>0) { return attrs[0].Description; //返回当前描述 } } return en.ToString(); }
调用代码
EnumService.GetDescription(UploadState.UploadSuccess)
2、指定名称获取特性描述