Func< T>注入Windsor容器 [英] Func<T> injecting with Windsor container
本文介绍了Func< T>注入Windsor容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public MessageBus(IClientRepository clientRepository,Func< IMessagesProcessor> messagesProcessorFactoryMethod)
{
this.clientRepository = clientRepository;
this.messagesProcessorFactoryMethod = messagesProcessorFactoryMethod;
}
如何注入 Func< IMessagesProcessor> messagesProcessorFactoryMethod
解决方案
container.Register(Component.For&FunC&Foo>>()。Instance(f));
这是一个经过单元测试,演示了这个概念:
[TestMethod]
public void Test2()
{
Func< string> f =()=> 你好,世界;
var container = new WindsorContainer();
container.Register(Component.For< Func< string>>()。Instance(f));
var resolvedFunc = container.Resolve< Func< string>>();
Assert.AreEqual(Hello world,f());
}
Here is a code excerpt from AspComet project that works with Autofac.
public MessageBus(IClientRepository clientRepository, Func<IMessagesProcessor> messagesProcessorFactoryMethod)
{
this.clientRepository = clientRepository;
this.messagesProcessorFactoryMethod = messagesProcessorFactoryMethod;
}
How can I inject "Func<IMessagesProcessor> messagesProcessorFactoryMethod
" with Windsor, is it possible?
Thanks.
解决方案
container.Register(Component.For<Func<Foo>>().Instance(f));
Here's a passing unit test that demonstrates the concept:
[TestMethod]
public void Test2()
{
Func<string> f = () => "Hello world";
var container = new WindsorContainer();
container.Register(Component.For<Func<string>>().Instance(f));
var resolvedFunc = container.Resolve<Func<string>>();
Assert.AreEqual("Hello world", f());
}
这篇关于Func< T>注入Windsor容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文