溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#怎么實(shí)現(xiàn)工廠接口

發(fā)布時(shí)間:2021-08-31 18:31:34 來源:億速云 閱讀:158 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要講解了“C#怎么實(shí)現(xiàn)工廠接口”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“C#怎么實(shí)現(xiàn)工廠接口”吧!

工廠接口

復(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");

感謝各位的閱讀,以上就是“C#怎么實(shí)現(xiàn)工廠接口”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對C#怎么實(shí)現(xiàn)工廠接口這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI