CastleWindsor 使用说明
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) { } } }