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 ...
随机推荐
- Quartz .net 一直运行失败
使用了新的: Quartz.NET 2.6.2 https://github.com/quartznet/quartznet/releases IScheduler scheduler = StdSc ...
- 移动端滑动时页面惯性滑动overflow-scrolling: touch
-webkit-overflow-scrolling:auto | touch; auto: 普通滚动,当手指从触摸屏上移开,滚动立即停止 touch:滚动回弹效果,当手指从触摸屏上移开,内容会保持一 ...
- C++标准库类模板vector
vector是C++标准库STL中的一个重要的类模板,相当于一个更加健壮的,有很多附加能力的数组 使用vector前首先要包含头文件 #include<vector> 1.vector的 ...
- Zookeeper选举算法原理
Zookeeper选举算法原理 Leader选举 Leader选举是保证分布式数据一致性的关键所在.当Zookeeper集群中的一台服务器出现以下两种情况之一时,需要进入Leader选举. (1) 服 ...
- 当页面有多个js文件时,应如何引入?
1. 我们知道如果一个页面有多个js文件,并且这些js文件有的还有依赖关系的时候,我们就要特别注意他们之间的引入顺序,否则就会报错. 如:一个js文件依赖jquery,我们就要先引入jquery,然后 ...
- OC总结 【OC基础语法相关知识】
m是OC源文件扩展名,入口点也是main函数,第一个OC程序: #import <Foundation/Foundation.h> int main(int argc, const cha ...
- OpenCV文本图像的旋转矫正
用户在使用Android手机拍摄过程中难免会出现文本图像存在旋转角度.这里采用霍夫变换.边缘检测等数字图像处理算法检测图像的旋转角度,并根据计算结果对输入图像进行旋转矫正. 首先定义一个结构元素,再通 ...
- UML 依赖\泛化\关联\实现\聚合\组合的 Java实现
在类图中,类与类之间的关系主要有一下几种: 泛化关系:(就是继承) public class Employee { } public class SaleEmployee extends Employ ...
- python: local variable 'xxx' referenced before assignment
问题发现 xxx = 23 def PrintFileName(strFileName): if xxx == 23: print strFileName xxx = 24 PrintFileName ...
- php操作Excel
php操作Excel 1.new PHPExcel对象$objPHPExcel = new PHPExcel(); 2表的初始化设置$objPHPExcel->getProperties()-& ...