zoukankan      html  css  js  c++  java
  • c# 调用服务返回结果模板化

    一般我们返回一个结果,主要有返回值,执行结果信息,所以定义一个类

     public  class QuestResult
        {

            /// <summary>
            /// 返回值
            /// </summary>
            public object Result { get; set; }

           /// <summary>
           /// 结果编码
           /// </summary>
            public ErrorCode Error { get; set; }

           /// <summary>
           /// 结果编码描述
           /// </summary>
            public string ErrorMsg { get; set; }

            /// <summary>
            /// 结果附近信息
            /// 主要是异常信息或者其它描述
            /// 例如:结果被截取
            /// </summary>
            public string ReslutMsg { get; set; }


        }

    对于返回的结果编码,采用枚举型方便扩展,而且可以为每个结对应的固定一个描述信息

    public enum ErrorCode
        {

            /// <summary>
            /// 成功
            /// </summary>
            /// 
            [Description("执行成功")]
            Sucess,

            /// <summary>
            ///执行超时
            /// </summary>
            /// 
            [Description("执行超时")]
            TimeOut,

            /// <summary>
            /// 执行异常
            /// </summary>
            /// 
            [Description("执行异常")]
            Exception,

            /// <summary>
            /// 结果被截取
            /// </summary>
            /// 
            [Description("结果被截取")]
            Truncate,

        }

    再启用一个扩展方法,获取枚举的描述信息

      /// </summary>
       public static class CommonExtend
        {
            /// <summary>
            /// 枚举描述
            /// </summary>
            private static Dictionary<string, string> dicEnum = new Dictionary<string, string>();
            /// <summary>
            /// 枚举描述特性获取信息
            /// </summary>
            /// <param name="value">枚举</param>
            /// <param name="isNameInstend">没有特性时是否直接使用字段名称</param>
            /// <returns></returns>
            public static string EnumDescription(this Enum value,bool isNameInstend=false)
            {
                Type type = value.GetType();
                string name = Enum.GetName(type, value);
                if (name == null)
                {
                    return null;
                }
                string description = "";
                if (dicEnum.TryGetValue(type.FullName+"_"+name,out description))
                {
                    return description;
                }
                FieldInfo field = type.GetField(name);
                DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (attribute == null && isNameInstend == true)
                {
                    return name;
                }
                //
                if(attribute!=null)
                {
                    dicEnum[type.FullName + "_" + name] = attribute.Description;
                }
                return attribute == null ? null : attribute.Description;
            }
        }

    这样就方便获取所有信息了,结果类中定义的ErrorMsg不是编码描述,而是在执行错误后的Exception信息。

    这样一个类似标准的返回结果模板就差不多了。

  • 相关阅读:
    【POJ 1958】 Strange Towers of Hanoi
    【HNOI 2003】 激光炸弹
    【POJ 3263】 Tallest Cow
    【POJ 2689】 Prime Distance
    【POJ 2777】 Count Color
    【POJ 1995】 Raising Modulo Numbers
    【POJ 1845】 Sumdiv
    6月16日省中集训题解
    【TJOI 2018】数学计算
    【POJ 1275】 Cashier Employment
  • 原文地址:https://www.cnblogs.com/jinyu20180311/p/10312362.html
Copyright © 2011-2022 走看看