Castle windsor 温莎城堡-注册组件
我创建了一个通用的静态类,用于注册解决方案范围内的组件Castle windsor 温莎城堡-注册组件,castle-windsor,windsor-3.0,Castle Windsor,Windsor 3.0,我创建了一个通用的静态类,用于注册解决方案范围内的组件 private static readonly IWindsorContainer Container = new WindsorContainer(); public static void Register<I, T>() where T : I { Container.Register( Component.For<I>().ImplementedBy<T
private static readonly IWindsorContainer Container = new WindsorContainer();
public static void Register<I, T>() where T : I
{
Container.Register(
Component.For<I>().ImplementedBy<T>().LifeStyle.Transient
);
}
private static readonly IWindsorContainer=new WindsorContainer();
公共静态无效寄存器(),其中T:I
{
集装箱。登记(
Component.For().ImplementedBy().LifeStyle.Transient
);
}
但是,我无法编译它。有什么想法吗
类型“I”必须是引用类型,才能将其用作泛型类型或方法“Castle.MicroKernel.Registration.Component.For()”中的参数“TService”如果要解析已注册类型,应使用与
IWindsorContainer
相同的实例
如果使用另一个
IWindsorContainer
实例,类型将不会被解析,正如警告所示,I
需要是引用类型,因此它需要一个泛型约束来约束它
public static void Register<I, T>() where T : I where I : class
{
Container.Register(
Component.For<I>().ImplementedBy<T>().LifestyleTransient());
}
然后在实例化容器时
var container = new WindsorContainer();
// find all installers in the current assembly and use them to register
// services with the container
container.Install(FromAssembly.This());
到底什么是不起作用的呢?你能提供一个失败的测试来提供更多的洞察力吗。如果需要的话,我可以编写一个测试来证明我的答案。在Register方法中,如果不是T:I,正如T实现I一样,根据Component.For().ImplementedBy()而不是“where I:T”,我已经更新了我的问题,使之更精确,并修复了我以前的输入错误“I:T”。
var container = new WindsorContainer();
// find all installers in the current assembly and use them to register
// services with the container
container.Install(FromAssembly.This());