C#可以减少或不使用switch有什么方法

 更新时间:2013年03月08日 14:43:04   作者:  
减少或不使用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的应用):
复制代码 代码如下:

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

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

IGetFactory get = (IGetFactory)Assembly.Load("程序集名称").CreateInstance("Insus.NET." + mothedName + "Factory");

相关文章

  • asp.net实现从Txt文件读取数据到数据视图的方法

    asp.net实现从Txt文件读取数据到数据视图的方法

    这篇文章主要介绍了asp.net实现从Txt文件读取数据到数据视图的方法,涉及asp.net针对文本文件的遍历操作与DataView的写入操作相关技巧,需要的朋友可以参考下
    2015-12-12
  • Visual Studio Debugger七个鲜为人知的小功能

    Visual Studio Debugger七个鲜为人知的小功能

    这篇文章主要为大家详细介绍了Visual Studio Debugger七个鲜为人知的小功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • Razor模板引擎简单介绍

    Razor模板引擎简单介绍

    这篇文章主要为大家详细介绍了Razor模板引擎的简单资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • asp.net 参数不同共用一个页面的实现方法

    asp.net 参数不同共用一个页面的实现方法

    本文为大家介绍下asp.net参数不同如何共用一个页面,感兴趣的朋友不要错过
    2013-12-12
  • .NET Core下使用Kafka的方法步骤

    .NET Core下使用Kafka的方法步骤

    这篇文章主要介绍了.NET Core下使用Kafka的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • ASP.NET中日历控件和JS版日历控件的使用方法(第5节)

    ASP.NET中日历控件和JS版日历控件的使用方法(第5节)

    这篇文章主要为大家详细介绍了ASP.NET中日历控件和JS版日历控件的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2015-08-08
  • ASP.NET操作Word的IIS权限设置

    ASP.NET操作Word的IIS权限设置

    检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。
    2011-02-02
  • Asp.Net 网站性能优化之缓字决 (上) 缓冲写数据

    Asp.Net 网站性能优化之缓字决 (上) 缓冲写数据

    通常情况下Asp.Net 网站的底层数据存储都是关系数据库,关系数据库资源比较昂贵,而且也很容易造成瓶颈。缓字决文章就是为大家介绍如何有效使用缓存,异步写缓冲数据库的压力,从而保证网站的性能。
    2010-06-06
  • .NET之生成数据库全流程实现

    .NET之生成数据库全流程实现

    这篇文章主要介绍了.NET之生成数据库全流程实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • Asp.Net MVC 分页、检索、排序整体实现代码

    Asp.Net MVC 分页、检索、排序整体实现代码

    很多时候需要这样的功能,对表格进行分页、排序和检索。本篇文章主要介绍了Asp.Net MVC 分页、检索、排序整体实现,有兴趣的可以了解一下。
    2017-01-01

最新评论