博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过反射实现IOC功能
阅读量:4034 次
发布时间:2019-05-24

本文共 3458 字,大约阅读时间需要 11 分钟。

闲来没事,自己就想着根据反射可以自己写一个简易的IOC组件。IOC组件说白了就是根据反射实例化对应的接口。废话不多说,开始说说我的解决方案。

1、项目结构图:

  • IOCTest为web MVC项目。

  • Common 通过配置文件实例化对应的接口

  • IBLL定义的接口

  • BLL实现接口

2、引用

  1. IOCTest项目引用IBLL、Common项目,不能引用BLL项目,这样就使IOCTest项目只依赖接口。

  2. BLL项目引用IBLL并实现接口

  3. 修改BLL项目dll生成路径,使其DLL生成到IOCTest项目的Bin目录下,如下图设置

 

3、下面我们来看具体的实现

(1)在IBLL层的IHelloWord.cs类中我们定义一个接口,代码如下

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace IBLL{  public  interface IHelloWord    {      string SayHello(string Name);    }}
br

(2)BLL层实现接口‍

using System;using System.Collections.Generic;using System.Linq;using System.Text;using IBLL;namespace BLL{    public class HelloWord:IHelloWord    {        #region IHelloWord 成员        public string SayHello(string Name)        {            return  "Hello_"+Name;        }        #endregion    }  }

(3)在IOCTest 项目的根目录Web.config下做如下配置(把HelloWord和IHelloWord对应起来):

说明:  key值为接口的全称(命名空间+类名),value值为实现接口的类,两部分组成,逗号前面为生成的dll名称,逗号后面为类名全称(命名空间+类名)。

(4)Common 项目的IOCReflecter.cs类根据配置文件获取对应接口的实例化对象,代码实现如下

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;using System.Reflection;using System.Configuration;namespace Common{    public class IOCReflecter    {        private static Hashtable s_type=null;        static IOCReflecter()        {            s_type = new Hashtable();        }        public static T CreateIntance
() { Type t=typeof(T);//key Type type = s_type[t] as Type;//value if (type == null) { string[] AssemblyInfos = ConfigurationManager.AppSettings[t.FullName].Split(','); type = Assembly.Load(AssemblyInfos[0]).GetType(AssemblyInfos[1]); s_type.Add(t, type); } return (T)CreateObject(type) ; } ///
/// 根据typeName获取Type对象 /// ///
///
public static Type GetType(string typeName) { if (string.IsNullOrWhiteSpace(typeName)) { return null; } return Type.GetType(typeName); } ///
/// 获取对象 /// ///
///
private static object CreateObject(Type t) { if (t == null) { return null; } //查找没有参数的构造函数 //如果需要初始化带参数的构造函数 t.GetConstructors() 获取所有的构造函数 ,it.GetParameters()获取构造函数所有的参数, ConstructorInfo NonParameterConstructors= t.GetConstructors().Where(it=>it.GetParameters().Length==0).FirstOrDefault(); if (NonParameterConstructors == null) { throw new Exception( t.FullName+"必须有一个无参数或默认的构造函数"); } //调用数构造函数创建对象 return t.InvokeMember(null, BindingFlags.CreateInstance, null, null, null); } }}

(5)测试

在IOCTest项目Controllers中添加HomeController.cs文件,代码如下

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using IBLL;using Common;namespace IOCTest.Controllers{    public class HomeController : Controller    {        //        // GET: /Home/        public ActionResult Index()        {            //获取实例化对象            IHelloWord hello = IOCReflecter.CreateIntance
(); ViewBag.Message = hello.SayHello("eric"); return View(); } }}
@{    ViewBag.Title = "Index";}

@ViewBag.Message

最后上一张截图:

到此结束

转载地址:http://prydi.baihongyu.com/

你可能感兴趣的文章
Redis中常用命令
查看>>
spring下载
查看>>
读取request流
查看>>
微信消息模板的配置
查看>>
Spring框架结合Quartz实现任务调度实例
查看>>
Quartz Cron表达式 在线生成器
查看>>
struts2中action接收参数的3种方法
查看>>
java获取随机数
查看>>
url中传递中文参数时的转码与解码
查看>>
百度Ueditor设置允许上传的图片格式及大小
查看>>
java图形验证码生成工具类及web页面校验验证码
查看>>
微信菜单的配置
查看>>
移动端的emoji表情符号插入MySQL数据库失败
查看>>
mysql对指定列进行排名
查看>>
Eclipse安装FindBugs插件
查看>>
联想T440怎么把原装Win8或Win10换成Win7系统
查看>>
PowerDesigner将物理数据模型图生成图片
查看>>
PowerDesigner创建物理数据模型(PDM)
查看>>
PowerDesigner:导入SQL脚本
查看>>
PowerDesigner使用教程
查看>>