c# 依赖注入之---反射(转)
详细请看http://www.cnblogs.com/leoo2sk/archive/2009/06/17/1504693.html
定义一个接口,和两个类(实现该接口)
IButton:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ReflectionInjection
{
internal interface IButton
{
string ShowInfo();
}
}
WindowsButton:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ReflectionInjection
{
internal sealed class WindowsButton : IButton
{
public string Description { get; private set; }
public WindowsButton()
{
this.Description = "Windows风格按钮";
}
public string ShowInfo()
{
return this.Description;
}
}
}
MacButton:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ReflectionInjection
{
internal sealed class MacButton : IButton
{
public String Description { get; private set; }
public MacButton()
{
this.Description = " Mac风格按钮";
}
public string ShowInfo()
{
return this.Description;
}
}
}
ReflectionFactory:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml; namespace ReflectionInjection
{
internal static class ReflectionFactory
{
private static string _buttonType;
static ReflectionFactory()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Environment.CurrentDirectory + "\\Config.xml");
XmlNode xmlNode = xmlDoc.ChildNodes[].ChildNodes[];
_buttonType = xmlNode.ChildNodes[].Value; }
public static IButton MakeButton()
{
return Assembly.Load("ReflectionInjection").CreateInstance("ReflectionInjection." + _buttonType) as IButton;
}
}
}
主入口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ReflectionInjection
{
class Program
{
static void Main(string[] args)
{
//ReflectionFactory
IButton button = ReflectionFactory.MakeButton();
Console.WriteLine("创建 " + button.ShowInfo());
Console.ReadLine();
}
}
}
xml:
<?xml version="1.0" encoding="utf-8" ?>
<config>
<window>MacWindow</window>
<button>MacButton</button>
<textBox>MacTextBox</textBox>
</config>
c# 依赖注入之---反射(转)的更多相关文章
- Laravel框架下容器Container 的依赖注入和反射应用
依赖注入,简单说是把类里头依赖的对象,置于类外头,即客户端调用处.相当于把类与类解耦. 一个简单的例子: class A { public function __construct() { // 这种 ...
- PHP 在Swoole中使用双IoC容器实现无污染的依赖注入
简介: 容器(container)技术(可以理解为全局的工厂方法), 已经是现代项目的标配. 基于容器, 可以进一步实现控制反转, 依赖注入. Laravel 的巨大成功就是构建在它非常强大的IoC容 ...
- 采用dom4j和反射模拟Spring框架的依赖注入功能
Spring的依赖注入是指将对象的创建权交给Spring框架,将对象所依赖的属性注入进来的行为.在学习了dom4j后,其实也可以利用dom4j和反射做一个小Demo模拟Spring框架的这种功能.下面 ...
- 基于反射的通过set方法的依赖注入,可以看成一种设计模式,自己来用
非常好用,在properties文件中配置字符串和类名之间的对应,在程序里读取文件,找到类名,通过反射,达到调用set方法的目的,然后直接将自己的指向其他类的对象的引用赋值,指向实体对象. 比如use ...
- laravel中如何利用反射实现依赖注入
依赖注入 在一个类中经常会依赖于其他的对象,先看一下经典的写法 class Foo { public $bar; public function __construct() { $this->b ...
- PHP类的反射和依赖注入
/** * Class Point */ class Point { public $x; public $y; /** * Point constructor. * @param int $x ho ...
- 【Java】利用注解和反射实现一个"低配版"的依赖注入
在Spring中,我们可以通过 @Autowired注解的方式为一个方法中注入参数,那么这种方法背后到底发生了什么呢,这篇文章将讲述如何用Java的注解和反射实现一个“低配版”的依赖注入. 下面是我们 ...
- PHP反射机制实现自动依赖注入
依赖注入又叫控制反转,使用过框架的人应该都不陌生.很多人一看名字就觉得是非常高大上的东西,就对它望而却步,今天抽空研究了下,解开他它的神秘面纱.废话不多说,直接上代码: /* * * * 工具类,使用 ...
- C#反射与特性(六):设计一个仿ASP.NETCore依赖注入Web
目录 1,编写依赖注入框架 1.1 路由索引 1.2 依赖实例化 1.3 实例化类型.依赖注入.调用方法 2,编写控制器和参数类型 2.1 编写类型 2.2 实现控制器 3,实现低配山寨 ASP.NE ...
随机推荐
- 完美解决读取Excel的数字单元格时Cannot get a STRING value from a NUMERIC cell 报错处理
我使用的是Poi(最新的4.1.0)方式读取Excel ,我的方法如下: 在打印cell内容时,抛出下面的错误 Exception in thread "main" java.la ...
- 【爬虫】-xpath语法熟悉及实战
本文为自学记录,部分内容转载于 w3school python3网络爬虫实战 知乎专栏:写点python 如有侵权,请联系删除. 语法 1.选取节点 XPath 使用路径表达式在 XML 文档中选取节 ...
- 宜人贷项目里-----正则匹配input输入月份规则
在标签上可以直接进行校验如下,如果只调数字键盘type=number不好用可以用type=tel <input name="creditDate" oninput=" ...
- asp.net WebService技术简介
1.什么是web service? 这里借助百度百科专业的解释:web service是一个平台独立的.低耦合的.自包含的.基于可编程的web应用程序(说简单点,就是使用web service继续不需 ...
- CMS: DNN And Umbraco
在比较了众多CMS系统后,还是把焦点定在DNN和Umbraco两个系统上,这两个系统都可以使用UserControl扩展自己需要的功能. DNN的架构比较复杂,Module.Skin.Containe ...
- javascript记住用户名和登录密码
javascript记住用户名和登录密码 下面主要通过代码给大家展示下javascript记住用户名和登录密码,具体代码内容请看下文. <script type="text/javas ...
- MyBatis逆向工程详细教程
1 导入逆向工程到eclipse中 2 修改配置文件 注意修改以下几点: 修改要生成的数据库表 pojo文件所在包路径 Mapper所在的包路径 配置文件如下: <?xml version=&q ...
- <? extends A> 和 <? super A> 的意思
/** * @author Lee Boynton * @author Arthur van Hoff * @author Martin Buchholz * @author Ulf Zibis * ...
- step7: 输出到json文件
调用scrapy自身的Exporter输出 编辑pipeline文件 import json from scrapy.exporters import JsonItemExporter #引入Expo ...
- Swift强制解析
IDE:Xcode Version7.3.1 Swift中"数据类型?"表示这是可选类型,即 某个常量或者变量可能是一个类型,也可能什么都没有,不确定它是否有值,也许会是nil. ...