简体   繁体   English

如何将UnityContainer作为参数传递给Unity IOC xml配置文件中的注册

[英]How do I pass the UnityContainer as a parameter to a registration in a Unity IOC xml configuration file

I have an implementation of a factory interface that uses an IOC container to create it's objects. 我有一个工厂接口的实现,该接口使用IOC容器创建其对象。

The container is a automatic property on the Factory, and ideally I would like to inject this property with same container where the factory is being registered. 该容器是Factory上的自动属性,理想情况下,我想将此属性与注册工厂的容器一起注入。

For clarity I have described what I would like it to look like below. 为了清楚起见,我在下面描述了我想要的样子。

<container name="MyContainer">
    <register type="IControllerFactory" mapTo="UnityControllerFactory">        
        <property name="UnityContainer" value="MyContainer" />
    </register>
</container>    

Of course I have registered a class after this that automatically injects this registration into it's IControllerFactory property. 当然,在此之后,我已经注册了一个类,该类会自动将此注册注入其IControllerFactory属性中。

Is there a way to do this? 有没有办法做到这一点?

The IUnityContainer registered with itself by default. 默认情况下,IUnityContainer向其自身注册。 You can just set up your constructor like this: 您可以像这样设置您的构造函数:

public UnityControllerFactory(IUnityContainer unityContainer) 
{
   this.unityContainer = unityContainer;
}

No addition configuration is needed.... the dependency will be resolved hassle-free. 无需其他配置。...依赖关系将轻松解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM