一、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 = , /// <summary>
/// 系统状态码
/// </summary>
[Description("系统状态码")]
[Display(Name = "失败", Description = "请求失败!")]
Error = , /// <summary>
/// 验签状态码
/// </summary>
[Description("验签状态码")]
[Display(Name = "部分参数不完整", Description = "部分参数不完整!")]
Incomplete = , /// <summary>
/// 系统状态码
/// </summary>
[Description("系统状态码")]
[Display(Name = "内部服务异常", Description = "内部服务异常!")]
Ex = , /// <summary>
/// 系统状态码
/// </summary>
[Description("验签状态码")]
[Display(Name = "timeStamp参数异常", Description = "timeStamp参数异常!")]
TimeStamp_Str = , /// <summary>
/// 系统状态码
/// </summary>
[Description("验签状态码")]
[Display(Name = "请求超时", Description = "请求超时!")]
Timeout = , /// <summary>
/// 系统状态码
/// </summary>
[Description("验签状态码")]
[Display(Name = "用户权限限制", Description = "用户权限限制!")]
Power = , /// <summary>
/// 系统状态码
/// </summary>
[Description("验签状态码")]
[Display(Name = "签名不合法", Description = "签名不合法!")]
Sign = , /// <summary>
/// 系统状态码
/// </summary>
[Description("区分城市状态码")]
[Display(Name = "未授权此城市", Description = "未授权此城市!")]
DisArea = , /// <summary>
/// 系统状态码
/// </summary>
[Description("系统状态码")]
[Display(Name = "远程服务器返回错误: (404) 未找到。", Description = "远程服务器返回错误: (404) 未找到!")]
NoFind = , /// <summary>
/// 系统状态码
/// </summary>
[Description("系统状态码")]
[Display(Name = "远程服务器返回错误: (500) 内部服务器错误。", Description = "远程服务器返回错误: (500) 内部服务器错误。")]
NoErr = , #region 房估估
/// <summary>
/// 房估估
/// </summary>
[Description("第三方")]
[Display(Name = "查询小区/地址不能为空", Description = "查询小区/地址不能为空!")]
VillageNull = , /// <summary>
/// 房估估
/// </summary>
[Description("房估估第三方")]
[Display(Name = "小区匹配", Description = "小区数据获取成功!")]
GetVillageOk = , /// <summary>
/// 房估估 ------------报错信息 用它的 暂不使用
/// </summary>
[Description("房估估第三方")]
[Display(Name = "小区匹配", Description = "小区获取数据失败!")]
GetVillageErr = , /// <summary>
/// 房估估
/// </summary>
[Description("房估估第三方")]
[Display(Name = "房估估小区评价", Description = "小区评价数据成功!")]
GetHouseEvalOk = , /// <summary>
/// 房估估
/// </summary>
[Description("房估估第三方")]
[Display(Name = "房估估小区评价", Description = "小区评价数据成功!")]
GetHouseEvalErr = ,
#endregion #region 同盾 /// <summary>
///
/// </summary>
[Description("征信第三方")]
[Display(Name = "请求凭证无效", Description = "请求凭证无效!")]
CertErr = , /// <summary>
/// 征信
/// </summary>
[Description("征信第三方")]
[Display(Name = "请求用户数据", Description = "请求用户数据成功!")]
GetReqCreditDatalOK = , /// <summary>
/// 征信
/// </summary>
[Description("征信第三方")]
[Display(Name = "请求用户数据", Description = "请求用户数据失败!")]
GetReqCreditDatalErr = , /// <summary>
/// 征信
/// </summary>
[Description("征信第三方")]
[Display(Name = "获取用户数据", Description = "获取用户数据成功!")]
GetCreditDataOK = , /// <summary>
/// 征信
/// </summary>
[Description("征信第三方")]
[Display(Name = "获取用户数据", Description = "获取用户数据失败!")]
GetCreditDataErr = , /// <summary>
/// 征信
/// </summary>
[Description("征信第三方")]
[Display(Name = "获取详情成功", Description = "获取详情成功!")]
GetCreditDetailOK = , /// <summary>
/// 征信
/// </summary>
[Description("征信第三方")]
[Display(Name = "小区地址有误", Description = "小区地址有误!")]
Getvillage = , /// <summary>
/// 征信
/// </summary>
[Description("征信第三方")]
[Display(Name = "获取数据详情", Description = "获取数据详情失败!")]
GetCreditDetailErr = , /// <summary>
/// 征信
/// </summary>
[Description("征信第三方")]
[Display(Name = "请求数据", Description = "请求数据详情成功!")]
GetReqCreditDetailOK = , /// <summary>
/// 征信
/// </summary>
[Description("征信第三方")]
[Display(Name = "请求数据", Description = "请求数据详情失败!")]
GetReqCreditDetailErr = , #endregion #region 房估估验证
/// <summary>
/// 系统状态码
/// </summary>
[Description("房估估验证")]
[Display(Name = "输入房产面积数据格式有误", Description = "输入房产面积数据格式有误!")]
Areaformat = ,
/// <summary>
/// 系统状态码
/// </summary>
[Description("房估估验证")]
[Display(Name = "输入层数据格式有误", Description = "输入层数据格式有误!")]
Floorformat = , /// <summary>
/// 系统状态码
/// </summary>
[Description("房估估验证")]
[Display(Name = "输入总楼层数据格式有误", Description = "输入总楼层数据格式有误!")]
TotalFloorformat = , /// <summary>
/// 系统状态码
/// </summary>
[Description("房估估验证")]
[Display(Name = "所在楼层大于总楼层数据有误", Description = "所在楼层大于总楼层数据有误!")]
TotalBigFloorformat = , #endregion #region 同盾验证
/// <summary>
/// 系统状态码
/// </summary>
[Description("同盾验证")]
[Display(Name = "身份证号格式不对", Description = "身份证号格式不对!")]
CardIdformat = , /// <summary>
/// 系统状态码
/// </summary>
[Description("同盾验证")]
[Display(Name = "手机号格式不对", Description = "手机号格式不对!")]
Telformat = , #endregion /// <summary>
/// 系统状态码
/// </summary>
[Description("评估")]
[Display(Name = "评估失败", Description = "评估失败!")]
EvelErr = ,
} /// <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 > ? attrs[].Description : enumValue.ToString();
} } }
二、解决方案
1、状态码(模块化区分)
/// <summary>
/// 上传状态
/// </summary>
public enum UploadState
{
[Description("上传失败")]
UploadFailed = -, [Description("正在上传")]
Uploading = , [Description("上传成功")]
UploadSuccess =
}
方法
/// <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>)
{
DescriptionAttribute[] attrs = memberInfos[].GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; //获取描述特性 if (attrs!=null&&attrs.Length>)
{
return attrs[].Description; //返回当前描述
}
}
return en.ToString();
}
调用代码
EnumService.GetDescription(UploadState.UploadSuccess)
2、指定名称获取特性描述