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. aspx基础开始

    <%@ Page Language="C#" Debug="true" trace="false" validateRequest=& ...

  2. magento head.phtml 加载<a target=_parent

    <script language="javascript">function alinks(){  var links = document.getElementsBy ...

  3. 一个简单的基于HTTP协议的屏幕共享应用

    HTTP协议可以能是应用层协议里使用最广泛并且用途最多样的一个了.我们一般使用HTTP协议来浏览网页,但是HTTP协议还用来做很多其它用途.对开发人员来讲很常见的一种就是用HTTP协议作为各种版本控制 ...

  4. Android如何使用API

    转自:http://www.cnblogs.com/vanezkw/archive/2012/07/03/2574559.html 本文针对Android开发如何使用API文档进行一些经验分享. 1. ...

  5. Zabbix之配置文件详解

    zabbix的配置文件一般有三种:zabbixserver的配置文件zabbix_server.confzabbixproxy的配置文件zabbix_proxy.confzabbix_agentd的配 ...

  6. 我也谈“the difference between Factory, Service, and Provider in Angular”

    看完这篇文章之后的理解与实践:原文地址:http://tylermcginnis.com/angularjs-factory-vs-service-vs-provider/ <!doctype ...

  7. jsp 获取cookie 的值的方法

    Cookie cookies[]=request.getCookies(); //读出用户硬盘上的Cookie,并将所有的Cookie放到一个cookie对象数组里面 Cookie sCookie=n ...

  8. ural1147 Shaping Regions

    Shaping Regions Time limit: 0.5 secondMemory limit: 64 MB N opaque rectangles (1 ≤ N ≤ 1000) of vari ...

  9. FZU 2122 又见LKity(KMP+返回所有匹配位置)

    基础kmp应用,找到所有匹配位置即可 #include<stdio.h> #include<string.h> #include<algorithm> #inclu ...

  10. Java时间操作(一):关于UTC格式时间处理

    Java中获取形如:20160811T122132+08 格式,可以通过如下方法 package com.mc.others; public class UTCTimeTest { @Test pub ...