开发者

How do you use multiple typeConfigs in Unity?

In unity, when configuring a type, you can specify more than o开发者_JAVA百科ne typeConfig if uniquely named like so:

<type type="IFoo" mapTo="ConcreteFoo">
    <typeConfig name="rainbows">
        <constructor>
            <param name="magic" parameterType="string">
                <value value="rainbows"/>
            </param>
        </constructor>
    </typeConfig>
    <typeConfig name="unicorns>
        <constructor>
            <param name="magic" parameterType="string">
                <value value="unicorns"/>
            </param>
        </constructor>
    </typeConfig>
</type>

When resolving, how do you specify which typeConfig, and consequently what gets injected to the constructor?


Supply the name in a DependencyAttribute, like this:

[Dependency("unicorns")]


When you call container.Resolve, you can pass a string. That string is the name that you used to configure a particular registration.

Also, when you configure registrations, you can specify the dependencyName on a registration to tell it which named registration you want the container to use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜