zoukankan      html  css  js  c++  java
  • C#用什么方法可以减少或不使用switch

    有在论坛看见一帖,《C#用什么方法可以减少或不使用switch

    Insus.NET的解决方法,是使用工厂方法来处理,可以创建一个工厂接口,然后每个方法设计为一个工厂类,并实现工厂接口。

    工厂接口:

    IGetFactory
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for IGetFactory
    /// </summary>
    namespace Insus.NET
    {
        public interface IGetFactory
        {
            string GetResult();
        }
    }


    Get工厂类:

    GetFactory
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for GetFactory
    /// </summary>
    namespace Insus.NET
    {
        public class GetFactory : IGetFactory 
        {
            public GetFactory()
            {
                //
                // TODO: Add constructor logic here
                //
            }
    
            public string GetResult()
            {
                return "get";
            }
        }
    }


    GetTest类:

    GetTestFactory
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for GetTestFactory
    /// </summary>
    namespace Insus.NET
    {
        public class GetTestFactory : IGetFactory 
        {
            public GetTestFactory()
            {
                //
                // TODO: Add constructor logic here
                //
            }
    
            public string GetResult()
            {
                return "gettest";
            }
        }
    }


    以及GetSet类:

    GetSetFactory
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for GetSetFactory
    /// </summary>
    namespace Insus.NET
    {
        public class GetSetFactory : IGetFactory 
        {
            public GetSetFactory()
            {
                //
                // TODO: Add constructor logic here
                //
            }
    
            public string GetResult()
            {
                return "getset";
            }
        }
    }


    因此你的代码最终变为:

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Insus.NET;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        public string Exec(string mothedName)
        {
            string ret = "";       
            
            //switch (mothedName)
            //{
            //    case "get":
            //        ret = get();
            //        break;
            //    case "get1":
            //        ret = gettest();
            //        break;
            //    //.....
            //    case "testget":
            //        ret = getrset();
            //        break;
            //}
    
            IGetFactory get = new GetTestFactory();  //这里是实现工厂类
            ret = get.GetResult();
            return ret;
        }
    
        //public string get()
        //{
        //    return "get";
        //}
    
        //public string gettest()
        //{
        //    return "gettest";
        //}
    
        //public string getrset()
        //{
        //    return "getset";
        //}
    }


    15:50修改补充如下
    上面的最终代码,无传入参数mothedName,怎样办,我们可以虑一下反射,如果改为反射击,那传入的参数需要规范一下方可以:

    "get" >>"Get";
    "get1" >>"GetTest"
    "testget" >> "GetSet"

     这样一改之后,就可以使用反射语法了,可以把

    IGetFactory get = new GetTestFactory();  //这里是实现工厂类


    改为(下面是asp.net的应用):

    Reflection
    IGetFactory get = (IGetFactory)Assembly.Load("App_Code").CreateInstance("Insus.NET." + mothedName + "Factory");


    如果在非asp.net下,可以把"App_Code"改为"程序集名称":

    View Code
    IGetFactory get = (IGetFactory)Assembly.Load("程序集名称").CreateInstance("Insus.NET." + mothedName + "Factory");
  • 相关阅读:
    Objective-C马路成魔【14-关键C语言功能】
    js正则表达式语法
    Python 得到Twitter所有用户friends和followers
    error:stray&#39;243&#39;in program
    VC各种方法获得的窗口句柄
    新东方雅思词汇---5.2
    php中this,self,parent三个关键字的区别辨析
    英语影视台词---一、少年派的奇幻漂流
    英语常用单词分类---1
    amazeui的表单开关插件的自定义事件必须添加.bootstrapSwitch 命名空间,给了我们什么启示
  • 原文地址:https://www.cnblogs.com/insus/p/2942653.html
Copyright © 2011-2022 走看看