文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>一步一步去掉这一类switch Case

一步一步去掉这一类switch Case

时间:2011-01-24  来源:彭杰斌

 

Get_MCODE_Desc public static string Get_MCODE_Desc(string MCODE)
        {
            switch (MCODE)
            {
                case "101000":
                    return "链路检测(Network Management)";

                case "101001":
                    return "业务前置机签到交易(Sign On)";

                case "101002":
                    return "业务前置机签退交易(Sign Off)";

                case "101010":
                    return "订购关系查询(Order Relationship Query)";

                case "101071":
                    return "子应用状态查询(Sub_Application status Query)";

                case "101011":
                    return "发卡请求(空中、写卡器)(Create Sub Application Request)";

                case "101012":
                    return "卡片操作结果通知(写卡器)(Terminal Card Operation Result)";

                case "101021":
                    return "业务前置机挂失通知(Prehost Report Of Loss)";

                case "101022":
                    return "解挂通知(Cancel Report Of Loss)";

                case "101031":
                    return "注销请求(空中、写卡器)(Cancellation Request)";

                case "101032":
                    return "废卡请求(Annul Card Request)";

                case "101041":
                    return "个人化信息更新请求(空中)(Update Sub_Application)";

                case "101051":
                    return "恢复企业初始主控密钥(Reload Keys)";

                case "101061":
                    return "信息服务通知(Message Send)";

                case "101062":
                    return "信息状态查询(Message Status Query)";

                case "101081":
                    return "机具状态数据上报(Terminal Status)";

                case "101091":
                    return "卡片操作请求(Card Operation Request)";

                case "102011":
                    return "空中卡片操作结果通知(Air Card Operation Result)";

                case "102021":
                    return "一卡通业务前置机应用停止通知(Prehost Application Stop)";

                case "102031":
                    return "信息查询请求(Message Query Request)";

                case "102041":
                    return "服务系统挂失通知(ServiceSystem Report Of Loss)";

                case "108001":
                    return "企业客户端查询前置状态(Query PreHost Status)";
            }
            return "未知交易码";
        }
    }

 

 

再来去掉的方法:

 

StrCodeDescription        /// <summary>
        /// 
        /// </summary>
        /// <param name="iCode">传入业务代码</param>
        /// <returns>返回业务描述</returns>
        public static string StrCodeDescription(int iCode)
        {
            string strResult;
            Dictionary<int, String>  mCodeDescription= new Dictionary<int, string>
                                                           {
                                                               {101000, "链路检测(Network Management)"},
                                                               {101001, "业务前置机签到交易(Sign On)"},
                                                               {101002, "业务前置机签退交易(Sign Off)"},
                                                               {101010, "订购关系查询(Order Relationship Query)"},
                                                               {101011, "发卡请求(空中、写卡器)(Create Sub Application Request)"},
                                                               {101012, "卡片操作结果通知(写卡器)(Terminal Card Operation Result)"},
                                                               {101021, "业务前置机挂失通知(Prehost Report Of Loss)"},
                                                               {101022, "解挂通知(Cancel Report Of Loss)"},
                                                               {101031, "注销请求(空中、写卡器)(Cancellation Request)"},
                                                               {101032, "废卡请求(Annul Card Request)"},
                                                               {101041, "个人化信息更新请求(空中)(Update Sub_Application)"},
                                                               {101051, "恢复企业初始主控密钥(Reload Keys)"},
                                                               {101061, "信息服务通知(Message Send)"},
                                                               {101062, "信息状态查询(Message Status Query)"},
                                                               {101071, "子应用状态查询(Sub_Application status Query)"},
                                                               {101081, "机具状态数据上报(Terminal Status)"},
                                                               {101091, "卡片操作请求(Card Operation Request)"},
                                                               {102011, "空中卡片操作结果通知(Air Card Operation Result)"},
                                                               {102021, "一卡通业务前置机应用停止通知(Prehost Application Stop)"},
                                                               {102031, "信息查询请求(Message Query Request)"},
                                                               {102041, "服务系统挂失通知(ServiceSystem Report Of Loss)"},
                                                               {108001, "企业客户端查询前置状态(Query PreHost Status)"}
                                                           };
                       
            strResult = "未知交易码";

            foreach (var item in mCodeDescription)
            {
                //int iSearch = Search(item., 0, 21, 11);
                if(item.Key == iCode)
                {
                    strResult = item.Value;
                }
            }

            return strResult;
        }

 

我们来测试一下运行情况

代码 Using Xunit;//用的Resharp 的Xunit插件

    public class Test01
    {
        [Fact]
        public void Test()
        {
            Stopwatch timer = new Stopwatch();
            decimal micro;

            timer.Start();
            string str = MCodeDesc.Get_MCODE_Desc("101021");
            Console.WriteLine(str);
            timer.Stop();

            micro = timer.Elapsed.Ticks / 10m;
            Console.WriteLine("Execution time was {0:F1} microseconds.", micro);

            timer = Stopwatch.StartNew();

            timer.Start();
            Console.WriteLine(MCodeDesc.StrCodeDescription(101021));
            timer.Stop();

            micro = timer.Elapsed.Ticks / 10m;
            Console.WriteLine("Execution time was {0:F1} microseconds.", micro);

            timer = Stopwatch.StartNew();

            timer.Start();
            Console.WriteLine(MCodeDesc.StrCodeDescription(101025));
            timer.Stop();

            micro = timer.Elapsed.Ticks / 10m;
            Console.WriteLine("Execution time was {0:F1} microseconds.", micro);
        }
    }

 

 

测试结果:

 

Test01.Test : Passed

业务前置机挂失通知(Prehost Report Of Loss)
Execution time was 3075.8 microseconds.
业务前置机挂失通知(Prehost Report Of Loss)
Execution time was 3645.3 microseconds.
未知交易码
Execution time was 4.6 microseconds.

 

还能优化吗?

 

StrCodeDescription
        /// <summary>
        /// 
        /// </summary>
        /// <param name="iCode">传入业务代码</param>
        /// <returns>返回业务描述</returns>
        public static string StrCodeDescription(int iCode)
        {
            string strResult;
            Dictionary<int, String>  mCodeDescription= new Dictionary<int, string>
                                                           {
                                                               {101000, "链路检测(Network Management)"},
                                                               {101001, "业务前置机签到交易(Sign On)"},
                                                               {101002, "业务前置机签退交易(Sign Off)"},
                                                               {101010, "订购关系查询(Order Relationship Query)"},
                                                               {101011, "发卡请求(空中、写卡器)(Create Sub Application Request)"},
                                                               {101012, "卡片操作结果通知(写卡器)(Terminal Card Operation Result)"},
                                                               {101021, "业务前置机挂失通知(Prehost Report Of Loss)"},
                                                               {101022, "解挂通知(Cancel Report Of Loss)"},
                                                               {101031, "注销请求(空中、写卡器)(Cancellation Request)"},
                                                               {101032, "废卡请求(Annul Card Request)"},
                                                               {101041, "个人化信息更新请求(空中)(Update Sub_Application)"},
                                                               {101051, "恢复企业初始主控密钥(Reload Keys)"},
                                                               {101061, "信息服务通知(Message Send)"},
                                                               {101062, "信息状态查询(Message Status Query)"},
                                                               {101071, "子应用状态查询(Sub_Application status Query)"},
                                                               {101081, "机具状态数据上报(Terminal Status)"},
                                                               {101091, "卡片操作请求(Card Operation Request)"},
                                                               {102011, "空中卡片操作结果通知(Air Card Operation Result)"},
                                                               {102021, "一卡通业务前置机应用停止通知(Prehost Application Stop)"},
                                                               {102031, "信息查询请求(Message Query Request)"},
                                                               {102041, "服务系统挂失通知(ServiceSystem Report Of Loss)"},
                                                               {108001, "企业客户端查询前置状态(Query PreHost Status)"}
                                                           };

//就在这里优化吧
            if (!mCodeDescription.TryGetValue(iCode, out strResult))
            {
                strResult = "未知交易码";
            }

            return strResult;
        }

 

 

再看看运行结果

业务前置机挂失通知(Prehost Report Of Loss)
Execution time was 3313.1 microseconds.
业务前置机挂失通知(Prehost Report Of Loss)
Execution time was 2776.1 microseconds.
未知交易码
Execution time was 4.2 microseconds.

 

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载