0
所以这里是这样的情况:当我在某处注入一个IUnityContainer
到构造函数中时,事情工作就好了。但是,我做什么,而不是为继承从UnityContainer
,即为什么Unity不能注入一个具体的容器实现而不是IUnityContainer?
class Services : UnityContainer
现在,当我注入Services
,我得到的类,但它的所有注册都将丢失:(
我当然可以,注入IUnityContainer
它转换为Services
(它的工作),但我宁愿只是注入Services
开始。
我在做什么错?
编辑:大众的需求,这里是一些代码:
class Services : UnityContainer
{
// some of my personal stuff here; nothing important
}
// later, in Program.cs
IUnityContainer uc = new Services();
services.RegisterType<Foo,Bar>();
Application.Run(services.Resolve<MainFrame>());
// and then, elsewhere
class Test
{
public Test(Services svcs)
{
var bar = svcs.Resolve<Foo>(); // this fails - no registration in svcs
}
// on the other hand, this works just fine:
public Test(IUnityContainer svcs)
{
var s = (Services)svcs;
var bar = s.Resolve<Foo>(); // works just fine
}
}
我不清楚你想完成什么。请给我们一些代码。 – jlvaquero
这是正常的行为。我不确定你的期望 – Jehof
服务是UnityContainer吗?你确定这是正确的吗?你为什么使用继承? –