C#可以減少或不使用switch有什么方法
更新時間:2013年03月08日 14:43:04 作者:
減少或不使用switch,大家有什么好的方法,使用工廠方法來處理,可以創(chuàng)建一個工廠接口,然后每個方法設計為一個工廠類,并實現(xiàn)工廠接口,感興趣的朋友可以了解下
Insus.NET的解決方法,是使用工廠方法來處理,可以創(chuàng)建一個工廠接口,然后每個方法設計為一個工廠類,并實現(xiàn)工廠接口。
工廠接口:
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";
}
}
}
因此你的代碼最終變?yōu)?/STRONG>:
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(); //這里是實現(xiàn)工廠類
ret = get.GetResult();
return ret;
}
//public string get()
//{
// return "get";
//}
//public string gettest()
//{
// return "gettest";
//}
//public string getrset()
//{
// return "getset";
//}
}
15:50修改補充如下:
上面的最終代碼,無傳入?yún)?shù)mothedName,怎樣辦,我們可以慮一下反射,如果改為反射擊,那傳入的參數(shù)需要規(guī)范一下方可以:
"get" >>"Get";
"get1" >>"GetTest"
"testget" >> "GetSet"
這樣一改之后,就可以使用反射語法了,可以把
IGetFactory get = new GetTestFactory(); //這里是實現(xiàn)工廠類
改為(下面是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");
工廠接口:
復制代碼 代碼如下:
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";
}
}
}
因此你的代碼最終變?yōu)?/STRONG>:
復制代碼 代碼如下:
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(); //這里是實現(xiàn)工廠類
ret = get.GetResult();
return ret;
}
//public string get()
//{
// return "get";
//}
//public string gettest()
//{
// return "gettest";
//}
//public string getrset()
//{
// return "getset";
//}
}
15:50修改補充如下:
上面的最終代碼,無傳入?yún)?shù)mothedName,怎樣辦,我們可以慮一下反射,如果改為反射擊,那傳入的參數(shù)需要規(guī)范一下方可以:
"get" >>"Get";
"get1" >>"GetTest"
"testget" >> "GetSet"
這樣一改之后,就可以使用反射語法了,可以把
復制代碼 代碼如下:
IGetFactory get = new GetTestFactory(); //這里是實現(xiàn)工廠類
改為(下面是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");
相關文章
.NET使用報表工具FastReport實現(xiàn)打印功能
這篇文章介紹了.NET使用報表工具FastReport實現(xiàn)打印功能的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-03-03
asp.net上傳Excel文件并讀取數(shù)據(jù)的實現(xiàn)方法
這篇文章主要給大家介紹了關于asp.net上傳Excel文件并讀取數(shù)據(jù)的實現(xiàn)方法,文中通過示例代碼以及圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-03-03
.Net語言Smobiler開發(fā)利用Gridview控件設計較復雜的表單
這篇文章主要為大家詳細介紹了.Net語言Smobiler開發(fā)利用Gridview控件設計較復雜的表單,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
為自己的ASP網(wǎng)站系統(tǒng)構(gòu)建一套標記語言
為自己的ASP網(wǎng)站系統(tǒng)構(gòu)建一套標記語言...2006-09-09

