详细请看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# 依赖注入之---反射(转)的更多相关文章

  1. Laravel框架下容器Container 的依赖注入和反射应用

    依赖注入,简单说是把类里头依赖的对象,置于类外头,即客户端调用处.相当于把类与类解耦. 一个简单的例子: class A { public function __construct() { // 这种 ...

  2. PHP 在Swoole中使用双IoC容器实现无污染的依赖注入

    简介: 容器(container)技术(可以理解为全局的工厂方法), 已经是现代项目的标配. 基于容器, 可以进一步实现控制反转, 依赖注入. Laravel 的巨大成功就是构建在它非常强大的IoC容 ...

  3. 采用dom4j和反射模拟Spring框架的依赖注入功能

    Spring的依赖注入是指将对象的创建权交给Spring框架,将对象所依赖的属性注入进来的行为.在学习了dom4j后,其实也可以利用dom4j和反射做一个小Demo模拟Spring框架的这种功能.下面 ...

  4. 基于反射的通过set方法的依赖注入,可以看成一种设计模式,自己来用

    非常好用,在properties文件中配置字符串和类名之间的对应,在程序里读取文件,找到类名,通过反射,达到调用set方法的目的,然后直接将自己的指向其他类的对象的引用赋值,指向实体对象. 比如use ...

  5. laravel中如何利用反射实现依赖注入

    依赖注入 在一个类中经常会依赖于其他的对象,先看一下经典的写法 class Foo { public $bar; public function __construct() { $this->b ...

  6. PHP类的反射和依赖注入

    /** * Class Point */ class Point { public $x; public $y; /** * Point constructor. * @param int $x ho ...

  7. 【Java】利用注解和反射实现一个"低配版"的依赖注入

    在Spring中,我们可以通过 @Autowired注解的方式为一个方法中注入参数,那么这种方法背后到底发生了什么呢,这篇文章将讲述如何用Java的注解和反射实现一个“低配版”的依赖注入. 下面是我们 ...

  8. PHP反射机制实现自动依赖注入

    依赖注入又叫控制反转,使用过框架的人应该都不陌生.很多人一看名字就觉得是非常高大上的东西,就对它望而却步,今天抽空研究了下,解开他它的神秘面纱.废话不多说,直接上代码: /* * * * 工具类,使用 ...

  9. C#反射与特性(六):设计一个仿ASP.NETCore依赖注入Web

    目录 1,编写依赖注入框架 1.1 路由索引 1.2 依赖实例化 1.3 实例化类型.依赖注入.调用方法 2,编写控制器和参数类型 2.1 编写类型 2.2 实现控制器 3,实现低配山寨 ASP.NE ...

随机推荐

  1. js获取窗口宽度、高度

    1.获取屏幕的高度和宽度(屏幕分辨率): window.screen.height window.screen.width 2.获取屏幕工作区域的高度和宽度(去掉状态栏): window.screen ...

  2. mysql for mac 上的安装及用DataGrip连接

    ---恢复内容开始--- 1.首先下载MySQL的mac版本,地址百度就行了. 2.这个时候需要注意安装的时候,弹出来的一个类似窗口,上面有提示默认密码,但是我当时就忘记了这个默认密码,如果你记住了默 ...

  3. Quartz .net 禁止并行触发

    DisallowConcurrentExecution 禁用同步执行防止一个job 同一时间执行多次. [DisallowConcurrentExecution] public class Order ...

  4. Ubuntu16.04+Cuda8.0+cuDNN6配置py-faster rcnn(转)

    原博客地址:https://blog.csdn.net/meccaendless/article/details/79557162 0前言Faster R-CNN是任少卿2015年底推出的目标检测算法 ...

  5. zookeeper伪分布集群配置

    1.上传tar文件zookeeper-3.4.12.tar.gz 2.解压zookeeper-3.4.12.tar.gz [root@localhost zookeeper]# .tar.gz 3.重 ...

  6. SpringCloud---声明式服务调用---Spring Cloud Feign

    1.概述 1.1 Spring Cloud Ribbon.Spring Cloud Hystrix的使用几乎是同时出现的,Spring Cloud提供了一个更高层次的封装这2个工具类框架:Spring ...

  7. Nearest Common Ancestors(LCA板子)

    题目链接:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 1000 ...

  8. 关于halo博客系统的使用踩坑——忘记登录密码

    踩坑: halo系统可以直接通过运行jar -jar halo-0.0.3.jar跑起来,也可以通过导入IDE然后运行Application的main方法跑起系统. h2数据库访问路径:http:// ...

  9. 【转】常用算法复习及实现(C++版)

    一.霍夫曼树实现 给定n个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman tree).哈夫曼树是带权路径长度最短的树,权值较大 ...

  10. (转)nginx location在配置中的优先级

    原文:https://www.bo56.com/nginx-location%E5%9C%A8%E9%85%8D%E7%BD%AE%E4%B8%AD%E7%9A%84%E4%BC%98%E5%85%8 ...