温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#怎么实现工厂接口

发布时间:2021-08-31 18:31:34 来源:亿速云 阅读:145 作者:chen 栏目:开发技术

这篇文章主要讲解了“C#怎么实现工厂接口”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#怎么实现工厂接口”吧!

工厂接口

复制代码 代码如下:


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

感谢各位的阅读,以上就是“C#怎么实现工厂接口”的内容了,经过本文的学习后,相信大家对C#怎么实现工厂接口这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI