1.引用DLL Castle.Core.dll  和Castle.Windsor.dll

2. 引用命名空间

using Castle.MicroKernel.Resolvers.SpecializedResolvers;
using Castle.Windsor;
using Castle.Windsor.Installer;
using Castle.MicroKernel.Registration;

3. 注册 和解析

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.MicroKernel.Resolvers.SpecializedResolvers;
using Castle.Windsor;
using Castle.Windsor.Installer;
using Castle.MicroKernel.Registration;
namespace CastleWindsor
{
/// <summary>
/// CastleWindsor 文档 https://github.com/castleproject/Windsor/blob/master/docs/registering-components-one-by-one.md
/// </summary>
class Program
{
static void Main(string[] args)
{
regic r = new regic();
r.Init(); Console.ReadLine(); } } public class regic
{
public void Init()
{
IWindsorContainer container = new WindsorContainer();
container.Install(
FromAssembly.This()
);
//下面为你要注册的组件,即注入容器的配置。 //“WindowsFormsApplication1” 为程序集名称,Form为要注册类的基类
// container.Register(AllTypes.FromAssemblyNamed("WindowsFormsApplication1")
//.BasedOn<Form>().WithService.DefaultInterfaces()); // “Tasks”为你的程序集名称,“Service”为你的“IService”接口的实现类
container.Register(Classes.FromAssemblyNamed("CastleWindsor").Pick()
.If(t => t.Name.EndsWith("Article"))
.WithService.DefaultInterfaces()); ///相同后缀名 自动注册 如 IOrderService -> OrderService
container.Register(Classes.FromAssemblyNamed("CastleWindsor").Pick()
.If(t => t.Name.EndsWith("Service"))
.WithService.DefaultInterfaces()); ///单个注册方法1
// container.Register(Component.For<IOrderService>().ImplementedBy<OrderService>()); ///单个注册方法2
// container.Register(Component.For(typeof(IOrderService)).ImplementedBy(typeof(OrderService))); //根据父类接口注册
container.Register(Classes.FromThisAssembly().BasedOn(typeof(IAnimal)).WithService.Base());
//注册泛型
container.Register(Component.For(typeof(IRepository<>)).ImplementedBy(typeof(Repository<>)));
var ent=container.Resolve<IArticle>();
Console.WriteLine(ent.GetList());
var o = container.Resolve<IOrderService>();
Console.WriteLine(o.GetListOrder()); //根据父类注册
IAnimal p = container.Resolve<IAnimal>();
Console.WriteLine(p.GetNameNew());
//泛型 IRepository<Animal> a = container.Resolve<IRepository<Animal>>(); //注意 Resolve的都是接口
Console.WriteLine( a.GetTypeNew());
//以service 结尾的都会被注册 IService ->Service
Domain.ITaskService taskService = container.Resolve<Domain.ITaskService>();
Console.WriteLine(taskService.GetList()); new regic1().test(container); ITestService itservice = container.Resolve<ITestService>();
itservice.say();
} } public class regic1 { public void test(IWindsorContainer container)
{
// container = new WindsorContainer(); var reperson = container.Resolve<IRepository<Person>>();
Console.WriteLine(reperson.GetTypeNew()+"this is test");
} } public interface ITestService
{
void say();
} public class TestService:ITestService
{
//会自动注入
private IRepository<Animal> _repositoryAnimal; public TestService(IRepository<Animal> repositoryAnimal)
{
_repositoryAnimal=repositoryAnimal;
} public void say()
{ Console.WriteLine("aaaaaa"+_repositoryAnimal.GetTypeNew());
}
} public class TestReg
{
public void Init(IWindsorContainer container)
{ } }
}

CastleWindsor 使用说明的更多相关文章

  1. Atitit.项目修改补丁打包工具 使用说明

    Atitit.项目修改补丁打包工具 使用说明 1.1. 打包工具已经在群里面.打包工具.bat1 1.2. 使用方法:放在项目主目录下,执行即可1 1.3. 打包工具的原理以及要打包的项目列表1 1. ...

  2. awk使用说明

    原文地址:http://www.cnblogs.com/verrion/p/awk_usage.html Awk使用说明 运维必须掌握的三剑客工具:grep(文件内容过滤器),sed(数据流处理器), ...

  3. “我爱背单词”beta版发布与使用说明

    我爱背单词BETA版本发布 第二轮迭代终于画上圆满句号,我们的“我爱背单词”beta版本已经发布. Beta版本说明 项目名称 我爱背单词 版本 Beta版 团队名称 北京航空航天大学计算机学院  拒 ...

  4. Oracle 中 union 和union all 的简单使用说明

    1.刚刚工作不久,经常接触oracle,但是对oracle很多东西都不是很熟.今天我们来了解一下union和union all的简单使用说明.Union(union all): 指令的目的是将两个 S ...

  5. Map工具系列-02-数据迁移工具使用说明

    所有cs端工具集成了一个工具面板 -打开(IE) Map工具系列-01-Map代码生成工具说明 Map工具系列-02-数据迁移工具使用说明 Map工具系列-03-代码生成BySQl工具使用说明 Map ...

  6. Map工具系列-03-代码生成BySQl工具使用说明

    所有cs端工具集成了一个工具面板 -打开(IE) Map工具系列-01-Map代码生成工具说明 Map工具系列-02-数据迁移工具使用说明 Map工具系列-03-代码生成BySQl工具使用说明 Map ...

  7. jQuery验证控件jquery.validate.js使用说明

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载 ...

  8. gdbsever 使用说明

    gdbsever 使用说明 在新塘N3292x平台下 编译 gdbsever ./configure --target=arm-linux --host=arm-linux arm-linux-gdb ...

  9. mongoVUE的增删改查操作使用说明

    mongoVUE的增删改查操作使用说明 一. 查询 1. 精确查询 1)右键点击集合名,再左键点击Find 或者直接点击工具栏上的Find 2)查询界面,包括四个区域 {Find}区,查询条件格式{& ...

随机推荐

  1. spark中groupByKey与reducByKey

    [译]避免使用GroupByKey Scala Spark 技术   by:leotse 原文:Avoid GroupByKey 译文 让我们来看两个wordcount的例子,一个使用了reduceB ...

  2. PHP中将ip地址转成十进制数的两种实用方法

    As we all know that long2ip works as ip1.ip2.ip3.ip4 (123.131.231.212) long ip => (ip1 * 256 * 25 ...

  3. git 管理多个私钥

    .ssh/config文件: host imspring hostname 182.92.153.2 IdentityFile ~/.ssh/id_rsa port 22

  4. hdu_5719_Arrange(脑洞题)

    题目连接:hdu_5719_Arrange 题意: 给你1-n这 n个数,设一个排列的第i个数为Ai, Bi为A1到Ai的最小值,Ci为C1到Ci的最大值,问你有多少种排列方式,然后输出取模后的答案 ...

  5. HDU 1204 基础DP 非连续字段的最大和

    其实这个感觉是一眼题,只不过我真心太菜了. 题目已经告诉你了,有m段,n个数字,那么我们就只需要dp[m][n]即可,然后最后的答案肯定是dp[m][]的那一行,所以其他行都是没有用的,因为我们可以把 ...

  6. java HTTP请求 DefaultHttpClient is deprecated

    最近在使用Apache的httpclient的时候,maven引用了最新版本4.3,发现Idea提示DefaultHttpClient等常用的类已经不推荐使用了,之前在使用4.2.3版本的时候,还没有 ...

  7. zf-关于查询机把index.jsp换成index_new.jsp页面之后把功能链接都改成新页面的简单方法

    一开始我都是找action 然后一个一个的改 把onmousedown="goURL('index.jsp')" 改成 onmousedown="goURL('index ...

  8. postgres 错误duplicate key value violates unique constraint 解决方案

    SELECT setval('tablename_id_seq', (SELECT MAX(id) FROM tablename)+1) 主要是:serial key其实是由sequence实现的,当 ...

  9. Warning: The Copy Bundle Resources build phase contains this target's Info.plist file 'yintingting_baisi/Info.plist'.

    处理方法: The INFOPLIST_FILE build setting specifies the name of the Info.plist associated with your tar ...

  10. make[1]: *** [/workopenwrt/trunk/staging_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/stamp/.tools_install_nnnnn] Error 2 make[1]: Leaving directory `/work/openwrt/trunk' make: *** [world]

    主要原因是编译时未连上网,编译时需要下载些插件,连接网后,重启下系统再编译下.