1.新建一个控制台应用程序 TestIOC

2.新增一个 IocFactory类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Reflection;
namespace TestIOC
{
public class IocFactory
{
private string _path;
private Dictionary<string, object> dict = new Dictionary<string, object>();
public IocFactory(string path)
{ this._path=path;
} public object GetObject(string id)
{ XElement xe = XElement.Load(_path); var v = from c in xe.Elements("object") select c; dict = v.ToDictionary(k => k.Attribute("id").Value, s => { string type = s.Attribute("type").Value;
Type ty = Type.GetType(type);
return Activator.CreateInstance(ty); } ); return null;
} }
}

3.新增一个 PersonDao类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace TestIOC
{
public class PersonDao
{ public PersonDao()
{
Console.WriteLine(" this is PersonDao");
} public PersonDao(string name)
{
Console.WriteLine(" this is PersonDao:"+name);
}
}
}

4.首先 新建一个object.xml

<?xml version="1.0" encoding="utf-8" ?>

<objects>

  <object id="PersonDao" type="TestIOC.PersonDao, TestIOC" />

</objects>

5.新控制台进行依赖注入

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace TestIOC
{
class Program
{
static void Main(string[] args)
{
string path = @"F:\Q\mytest\spring\TestIOC\TestIOC\object.xml";
IocFactory ioc = new IocFactory(path);
PersonDao pd = ioc.GetObject("PersonDao") as PersonDao; Console.WriteLine("直接映射"); Activator.CreateInstance(Type.GetType("TestIOC.PersonDao"));
Console.ReadLine();
}
}
}

6.显示效果

this is PersonDao

直接映射

this is PersonDao

源码下载

自己实现一个IOC(控制翻转,DI依赖注入)容器的更多相关文章

  1. Spring 04: IOC控制反转 + DI依赖注入

    Spring中的IOC 一种思想,两种实现方式 IOC (Inversion of Control):控制反转,是一种概念和思想,指由Spring容器完成对象创建和依赖注入 核心业务:(a)对象的创建 ...

  2. laravel服务容器(IOC控制反转,DI依赖注入),服务提供者,门脸模式

    laravel的核心思想: 服务容器: 容器:就是装东西的,laravel就是一个个的对象 放入:叫绑定 拿出:解析 使用容器的目的:这里面讲到的是IOC控制反转,主要是靠第三方来处理具体依赖关系的解 ...

  3. Spring专题2: DI,IOC 控制反转和依赖注入

    合集目录 Spring专题2: DI,IOC 控制反转和依赖注入 https://docs.spring.io/spring/docs/2.5.x/reference/aop.html https:/ ...

  4. Spring.Net控制翻转、依赖注入、面向切面编程

    Spring.Net快速入门:控制翻转.依赖注入.面向切面编程 Spring.Net主要功能: 1.IoC:控制翻转(Inversion of Control)  理解成抽象工厂翻转控制:就是创建对象 ...

  5. Spring.Net快速入门:控制翻转、依赖注入、面向切面编程

    Spring.Net主要功能: 1.IoC:控制翻转(Inversion of Control)  理解成抽象工厂翻转控制:就是创建对象的权利由开发人员自己控制New,转到了由容器来控制. 2.DI: ...

  6. Java Web实现IOC控制反转之依赖注入

    控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心. 控制反转一般分为两种类型,依赖注入 ...

  7. (转)Ioc控制反转和依赖注入

    转载地址:https://zhuanlan.zhihu.com/p/95869440 控制反转控制反转(Inversion of Control,简称IoC),是面向对象编程中的一种设计思想,其作用是 ...

  8. Spring的IOC控制反转和依赖注入-重点-spring核心之一

    IoC:Inverse of Control(控制反转): 读作"反转控制",更好理解,不是什么技术,而是一种设计思想,好比于MVC.就是将原本在程序中手动创建对象的控制权,交由S ...

  9. OO思想举例,控制翻转,依赖注入

    (转自kumaws,原帖地址:http://www.cnblogs.com/kumaws/archive/2009/04/06/from_interface_to_DependencyInjectio ...

  10. 如何构建一个轻量级级的DI(依赖注入)

    概念:依赖注入与IOC模式类似工厂模式,是一种解决调用者和被调用者依赖耦合关系的模式:它解决了对象之间的依赖关系,使得对象只依赖IOC/DI容器,不再直接相互依赖,实现松耦合,然后在对象创建时,由IO ...

随机推荐

  1. JSch - Java实现的SFTP

    JSch - Java实现的SFTP(文件上传详解篇)     JSch是Java Secure Channel的缩写.JSch是一个SSH2的纯Java实现.它允许你连接到一个SSH服务器,并且可以 ...

  2. SpringMVC中获得HttpRequest对象的方法

    1. 使用@autowired注入HttpRequest 2. 在方法中直接声明形参有HttpRequest即可. 3. 使用一个Listener,然后获取.

  3. Effective C++笔记之Item49【了解new-handler的行为】

    //---------------------------------------------------------// //------------------------------------ ...

  4. 移动端touch点穿(穿透)解决办法

    回答一 穿透(点穿)是在mobile各种浏览器上发生的常见的bug.可能是由click事件的延迟或者事件冒泡导致. 移动web开发常用的Zepto库中的touch和tap事件就会有点穿的bug(Zep ...

  5. How do JavaScript closures work?

    Like the old Albert Einstein said: If you can't explain it to a six-year-old, you really don't under ...

  6. Fourinone 作者博客 -集群复制

    http://my.oschina.net/fourinone/blog http://www.iteye.com/blogs/subjects/fourinone http://fourinone. ...

  7. 屌爆了的两个在线编辑网站runjs和jsbin

    作者:zccst HTML5+css3做的小游戏 http://timelineapp.pointstone.org/coreball/game.html 切图工具:http://kuaiqie.co ...

  8. 三种Dataase Mapping的系统架构

    ORM - O/R M - Object/Relational Mapping: A technique/idea used to map objects and thier individual r ...

  9. 牛掰啊,github+svn+FB进行项目开发

    刚刚头脑发热,用svn把自己的github的一个项目下载下来,然后在Flash Builder中导入,然后惊喜的发现项目居然被svn接管了,如图: 当然,你得给你的Flash Builder装SVN插 ...

  10. MAC apache 2.4 启用目录访问

    1. 打开 httpd.conf 文件,在Options 后面添加 "Indexes",如下: <Directory "/Users/ChenShuo/Docume ...