C#用什么方法可以减少或不使用switch
2013-03-22 20:16
826 查看
有在论坛看见一帖,《C#用什么方法可以减少或不使用switch》
Insus.NET的解决方法,是使用工厂方法来处理,可以创建一个工厂接口,然后每个方法设计为一个工厂类,并实现工厂接口。
工厂接口:
IGetFactory
Get工厂类:
GetFactory
GetTest类:
GetTestFactory
以及GetSet类:
GetSetFactory
因此你的代码最终变为:
View Code
15:50修改补充如下:
上面的最终代码,无传入参数mothedName,怎样办,我们可以虑一下反射,如果改为反射击,那传入的参数需要规范一下方可以:
"get" >>"Get";
"get1" >>"GetTest"
"testget" >> "GetSet"
这样一改之后,就可以使用反射语法了,可以把
改为(下面是asp.net的应用):
Reflection
如果在非asp.net下,可以把"App_Code"改为"程序集名称":
View Code
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");
相关文章推荐
- C#可以减少或不使用switch有什么方法
- C#用什么方法可以减少或不使用switch
- 在进行C#编程时候,有的时候我们需要判断一个字符串是否是数字字符串,我们可以通过以下两种方法来实现。 【方法一】:使用 try{} catch{} 语句。 我们可以在try语句块中试图
- 再论属性可以变成方法---C#中使用new关键字隐藏成员
- iconfont字体图标的使用方法--超简单! 我之前因为项目用bootstrap比较多,所以使用font awesome字体图标比较多,后来接触到了iconfont,发现想要的什么图标都有,还可以
- C#第2次试验(1).创建一个抽象类A,该类中包含一个求两个数之和抽象方法。创建一个子类B,在B中重写求和方法,且使用方法重载使得方法可以分别计算整数、双精度、字符串。
- C#判断字符串中是否包含一个子字符串是可以直接使用Contains()方法
- C#使用List.Clear()方法可以让GC回收内存吗?
- C#下 子类继承父类的虚方法后 使用override重写父类方法 和 不使用override重写有什么区别?
- java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用
- 用jquery可以用使用serialize()序列化表单值,那有没有什么方法可以将值填充到表单中呢? (一段不错的代码)
- java中有几种方法可以实现一个线程?用什么关键字修饰同步方法 stop()和suspend()方法为何不推荐使用?
- 【Java面试题】23 java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用?
- Java 有几程方法可以实现一个线程?用什么关键字修饰同步?stop()和suspend()为什么不推荐使用?
- 使用c#把一个32位整数按位相加最快的方法是什么?
- C#中的序列化和反序列化是什么、有什么作用、使用方法详解
- asp.net后台如何输出js脚本使用什么方法可以实现
- C#中的序列化和反序列化是什么、有什么作用、使用方法详解