C#可以減少或不使用switch有什么方法
更新時間:2013年03月08日 14:43:04 作者:
減少或不使用switch,大家有什么好的方法,使用工廠方法來處理,可以創(chuàng)建一個工廠接口,然后每個方法設(shè)計(jì)為一個工廠類,并實(shí)現(xiàn)工廠接口,感興趣的朋友可以了解下
Insus.NET的解決方法,是使用工廠方法來處理,可以創(chuàng)建一個工廠接口,然后每個方法設(shè)計(jì)為一個工廠類,并實(shí)現(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(); //這里是實(shí)現(xiàn)工廠類
ret = get.GetResult();
return ret;
}
//public string get()
//{
// return "get";
//}
//public string gettest()
//{
// return "gettest";
//}
//public string getrset()
//{
// return "getset";
//}
}
15:50修改補(bǔ)充如下:
上面的最終代碼,無傳入?yún)?shù)mothedName,怎樣辦,我們可以慮一下反射,如果改為反射擊,那傳入的參數(shù)需要規(guī)范一下方可以:
"get" >>"Get";
"get1" >>"GetTest"
"testget" >> "GetSet"
這樣一改之后,就可以使用反射語法了,可以把
IGetFactory get = new GetTestFactory(); //這里是實(shí)現(xiàn)工廠類
改為(下面是asp.net的應(yīng)用):
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");
工廠接口:
復(fù)制代碼 代碼如下:
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工廠類:
復(fù)制代碼 代碼如下:
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類:
復(fù)制代碼 代碼如下:
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類:
復(fù)制代碼 代碼如下:
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>:
復(fù)制代碼 代碼如下:
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(); //這里是實(shí)現(xiàn)工廠類
ret = get.GetResult();
return ret;
}
//public string get()
//{
// return "get";
//}
//public string gettest()
//{
// return "gettest";
//}
//public string getrset()
//{
// return "getset";
//}
}
15:50修改補(bǔ)充如下:
上面的最終代碼,無傳入?yún)?shù)mothedName,怎樣辦,我們可以慮一下反射,如果改為反射擊,那傳入的參數(shù)需要規(guī)范一下方可以:
"get" >>"Get";
"get1" >>"GetTest"
"testget" >> "GetSet"
這樣一改之后,就可以使用反射語法了,可以把
復(fù)制代碼 代碼如下:
IGetFactory get = new GetTestFactory(); //這里是實(shí)現(xiàn)工廠類
改為(下面是asp.net的應(yīng)用):
復(fù)制代碼 代碼如下:
IGetFactory get = (IGetFactory)Assembly.Load("App_Code").CreateInstance("Insus.NET." + mothedName + "Factory");
如果在非asp.net下,可以把"App_Code"改為"程序集名稱":
復(fù)制代碼 代碼如下:
IGetFactory get = (IGetFactory)Assembly.Load("程序集名稱").CreateInstance("Insus.NET." + mothedName + "Factory");
相關(guān)文章
ASP.NET?Core?WebApi返回結(jié)果統(tǒng)一包裝實(shí)踐記錄
本文主要是展示了針對ASP.NET Core WeApi結(jié)果統(tǒng)一返回格式的相關(guān)操作,通過示例我們一步一步的展示了完成這一目標(biāo)的不斷升級的實(shí)現(xiàn),雖然整體看起來比較簡單,但是卻承載著筆者一次又一次的思考升級2022-04-04VS2022?.NET5一鍵發(fā)布到遠(yuǎn)程騰訊云IIS服務(wù)器的詳細(xì)步驟
這篇文章主要介紹了VS2022?.NET5一鍵發(fā)布到遠(yuǎn)程騰訊云IIS服務(wù)器,首先需要添加服務(wù)器相關(guān)功能,文中通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04asp.net 生成數(shù)字和字母組合的隨機(jī)數(shù)
asp.net下生成數(shù)字跟字母組合的隨機(jī)數(shù),提高驗(yàn)證安全。2009-03-03asp.net ListView 數(shù)據(jù)綁定
asp.net ListView 數(shù)據(jù)綁定 實(shí)現(xiàn)代碼2009-01-01Entity?Framework使用配置伙伴創(chuàng)建數(shù)據(jù)庫
這篇文章介紹了Entity?Framework使用配置伙伴創(chuàng)建數(shù)據(jù)庫的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03asp.net core常見的4種數(shù)據(jù)加密算法
這篇文章主要介紹了asp.net core常見的4種數(shù)據(jù)加密算法,文中代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06