说我有以下课程
MyComponent : IMyComponent { public MyComponent(int start_at) {...} }
我可以通过xml使用castle windsor注册它的实例,如下所示
1
我将如何在代码中执行完全相同的操作?(注意,构造函数参数)
编辑:使用Fluent接口使用以下代码的答案:)
namespace WindsorSample { using Castle.MicroKernel.Registration; using Castle.Windsor; using NUnit.Framework; using NUnit.Framework.SyntaxHelpers; public class MyComponent : IMyComponent { public MyComponent(int start_at) { this.Value = start_at; } public int Value { get; private set; } } public interface IMyComponent { int Value { get; } } [TestFixture] public class ConcreteImplFixture { [Test] void ResolvingConcreteImplShouldInitialiseValue() { IWindsorContainer container = new WindsorContainer(); container.Register( Component.For() .ImplementedBy () .Parameters(Parameter.ForKey("start_at").Eq("1"))); Assert.That(container.Resolve ().Value, Is.EqualTo(1)); } } }