.net Windsor AddComponentance问题

.net Windsor AddComponentance问题,.net,castle-windsor,ioc-container,.net,Castle Windsor,Ioc Container,我已经使用xml配置文件成功地安装了castle windsor,一切正常。唯一的问题是,在方法上,我需要Windsor将现有实例传递给构造函数,所以我使用了 container.Kernel.AddComponentInstance<IMyClass>(MyClassInstance); container.Kernel.AddComponentInstance(MyClassInstance); 在Resolve方法之前,但这不起作用,因为Windsor基于xml配置文件创建

我已经使用xml配置文件成功地安装了castle windsor,一切正常。唯一的问题是,在方法上,我需要Windsor将现有实例传递给构造函数,所以我使用了

container.Kernel.AddComponentInstance<IMyClass>(MyClassInstance);
container.Kernel.AddComponentInstance(MyClassInstance);
在Resolve方法之前,但这不起作用,因为Windsor基于xml配置文件创建了IMyClass的新实例。除了我需要传递现有实例的这个特定方法之外,其他任何地方都可以使用这种行为。我怎样才能解决那个问题。尝试在web上查找文档,但到目前为止运气不佳:(。

您可以执行以下操作:

var instance = new MyClass();
container.Register(Component.For<IMyClass>().Instance(instance));
var instance=new MyClass();
Register(Component.For().Instance(Instance));
您可以执行以下操作:

var instance = new MyClass();
container.Register(Component.For<IMyClass>().Instance(instance));
var instance=new MyClass();
Register(Component.For().Instance(Instance));

对我来说效果不好。我想处理nhibernate会话..并且我想在测试时通过SQLite会话(手动实例化)除此之外,我希望通过xml配置文件映射会话。但这更为复杂,因为我想将数据层分解为多个逻辑模块和程序集,所以我必须进行更多重构,因为现在我有两个会话工厂,每个数据模块一个,所以我使用参数(在windsor xml配置上)对于需要的类。Thanx无论如何,你给了我一个很好的实验方法。对我来说效果不好。我想处理nhibernate会话。我想在测试时通过一个SQLite会话(手动实例化)除此之外,我希望通过xml配置文件映射会话。但这更为复杂,因为我想将数据层分解为多个逻辑模块和程序集,所以我必须进行更多重构,因为现在我有两个会话工厂,每个数据模块一个,所以我使用参数(在windsor xml配置上)不管怎样,你给了我一个好的实验方法。