---
title: "枚举类型帮助类  EnumHelper"
author: "*YH*"
date: "2019-09-02T11:29:42Z"
tags: []
categories: ["编程语言"]
slug: "enumhelper"
draft: false
lastmod: "2019-09-02T11:29:42Z"
id: 2444035
url: "https://yh-fly.iteye.com/blog/2444035"
---

项目中出现的问题： 项目中会有很多地方用到文字提醒，如果在每个页面都输入相同的文字，后期修改就比较麻烦。 延伸：如果是写api接口不仅要有提示，还得附带错误code值，不同业务类型，code值也应该有差异化 解决思路： 将所有的文字提醒写在一个枚举类型中，通过对枚举类型来管理。 好处：相同的提醒只需更改一次，项目中所有用到的地方都可以更改过来 方法： 首先新建枚举类 \[code="C#"] /// /// api 响应状态吗 /// public enum ResultCode { /// /// 操作成功 /// \[Description("成功")] Success = 10000, /// ///系统繁忙，请稍后再试 /// \[Description("系统繁忙，请稍后再试")] Faild = 10001, /// /// 登陆时效已过，请重新登录 /// \[Description("登录时效已过，请重新登录")] CustomerTokenError = 10002, /// /// 用户不存在 /// \[Description("用户不存在")] CustomerNotExist = 20001, /// /// 密码不能少于6位 /// \[Description("密码不能少于6位")] PasswordIsShort = 20002, /// /// 没有数据 /// \[Description("没有数据")] NoData = 30001, /// /// 没有更多数据 /// \[Description("没有更多数据")] NoMoreData = 30002, } /// /// api 响应实体 /// public class ResponseResult { //public string Status { get; set; } /// /// 响应状态码 /// public int Code { get; set; } /// /// 响应消息 /// public string Message { get; set; } /// /// 响应时间 /// public string Time { get; set; } /// /// 返回数据 /// public dynamic Result { get; set; } /// /// 根据枚举类型，返回错误代码 /// /// 返回响应码状态枚举 /// ResponseResult类 public static ResponseResult GenFaildResponse(ResultCode code = ResultCode.Faild) { return new ResponseResult { Code = (int)code, Message = code.GetDescription(), Time = DateTime.Now.ToString() }; } /// /// 执行失败，code：10001，返回错误的data /// /// data /// public static ResponseResult GenFaildResponse(dynamic data) { return new ResponseResult { Code = (int)ResultCode.Faild, Message = ResultCode.Faild.GetDescription(), Time = DateTime.Now.ToString(), Result = data }; } /// /// 返回执行失败消息，code：10001 msg=参数 /// /// 错误消息 /// API响应实体 public static ResponseResult GenFaildResponse(string msg) { return new ResponseResult { Code = (int)ResultCode.Faild, Message = msg, Time = DateTime.Now.ToString() }; } /// /// 执行成功后，返回code：10000 描述：执行成功及数据data /// /// 要返回的数据，可以为空 /// API响应实体 public static ResponseResult GenSuccessResponse(dynamic data = null) { return new ResponseResult { Code = (int)ResultCode.Success, Message = ResultCode.Success.GetDescription(), Time = DateTime.Now.ToString(), Result = data }; } } \[/code] 其次需要一个枚举帮助类，帮我们读取描述文件 \[code="C#"] public static class EnumHelper { /// /// 获取枚举变量值的 Description 属性 /// /// 枚举变量 /// 如果包含 Description 属性，则返回 Description 属性的值，否则返回枚举变量值的名称 public static string GetDescription(this Enum obj) { return GetDescription(obj, false); } /// /// 获取枚举变量值的 Description 属性 /// /// 枚举变量 /// 是否改变为返回该类、枚举类型的头 Description 属性，而不是当前的属性或枚举变量值的 Description 属性 /// 如果包含 Description 属性，则返回 Description 属性的值，否则返回枚举变量值的名称 public static string GetDescription(this Enum obj, bool isTop) { if (obj == null) { return string.Empty; } try { Type \_enumType = obj.GetType(); DescriptionAttribute dna = null; if (isTop) { dna = (DescriptionAttribute)Attribute.GetCustomAttribute(\_enumType, typeof(DescriptionAttribute)); } else { FieldInfo fi = \_enumType.GetField(Enum.GetName(\_enumType, obj)); dna = (DescriptionAttribute)Attribute.GetCustomAttribute( fi, typeof(DescriptionAttribute)); } if (dna != null && string.IsNullOrEmpty(dna.Description) == false) return dna.Description; } catch { } return obj.ToString(); } } \[/code] 这样就方便管理了，如果有更好的方法，欢迎大家分享，共同进步