如何使用 Unity 解析数组并将参数传递给数组中的一项?

发布于 2024-09-18 07:13:48 字数 1061 浏览 5 评论 0原文

这是我的工厂方法,它接受一个参数...

            container.RegisterInstance<Func<IProductInstance, IQuantityModifier[]>>(
            instance => container.Resolve<IQuantityModifier[]>());

现在,数组返回的其中一个项目在其构造函数中接受 IProductInsance 参数。我不知道如何让 Unity 传入参数,或者如果我将构造函数参数设为属性,如何让 Unity 设置该属性。再多的依赖覆盖、注入参数等似乎也没有任何作用。

当然,如果我正在解析单个实例,那么这两种情况都很容易,但对于数组,Unity 似乎无法完全处理每个项目。

有什么想法吗?我最终所做的是这样的事情...

            container.RegisterInstance<Func<IProductInstance, IQuantityModifier[]>>(
            instance =>
                {
                    var items = container.Resolve<IQuantityModifier[]>();

                    QuantityModifier item = items.OfType<QuantityModifier>().SingleOrDefault();

                    if (item != null)
                    {
                        item.ProductInstance = instance;
                    }

                    return items;
                };

我想理想情况下需要参数的项目将由工厂创建,但随后 Unity 必须将正确的值传递到工厂并执行它。

干杯,伊恩。

So here's my factory method which takes a parameter...

            container.RegisterInstance<Func<IProductInstance, IQuantityModifier[]>>(
            instance => container.Resolve<IQuantityModifier[]>());

Now one of the items returned by the array takes the IProductInsance parameter in its constructor. I can't figure out how to get Unity to pass the parameter in or, if I make the constructor argument a property instead, how to get Unity to set the property. No amount of dependency overrides, injection parameters etc. seem to do anything.

Of course both of these situations would be easy if I was resolving a single instance but with an array Unity doesn't seem to fully process each item.

Any ideas? What I've ended up doing is stuff like this...

            container.RegisterInstance<Func<IProductInstance, IQuantityModifier[]>>(
            instance =>
                {
                    var items = container.Resolve<IQuantityModifier[]>();

                    QuantityModifier item = items.OfType<QuantityModifier>().SingleOrDefault();

                    if (item != null)
                    {
                        item.ProductInstance = instance;
                    }

                    return items;
                };

I suppose ideally the item that requires the parameter would be created by a factory but then Unity would have to pass the correct value into the factory and execute it.

Cheers, Ian.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒几许 2024-09-25 07:13:48

遗憾的是,您在容器中遇到了错误。解析覆盖应该在这里做正确的事情。我怀疑这与此错误的根本原因相同: http://unity.codeplex.com/workitem/8777

我正在查看容器的源代码,问题出在 ArrayResolutionStrategy 中的这个方法中:

    private static object ResolveArray<T>(IBuilderContext context)
    {
        IUnityContainer container = context.NewBuildUp<IUnityContainer>();
        List<T> results = new List<T>(container.ResolveAll<T>());
        return results.ToArray();
    }

当前的覆盖集是当前构建上下文的一部分,而不是容器本身,因此当它获取容器并重新解决了上下文丢失的问题。

该死,我得想办法解决这个问题。

Sadly, you've hit a bug in the container. A resolve override should do the right thing here. I suspect it's the same underlying cause as this bug: http://unity.codeplex.com/workitem/8777

I'm looking in the source code for the container, the problem is in this method in ArrayResolutionStrategy:

    private static object ResolveArray<T>(IBuilderContext context)
    {
        IUnityContainer container = context.NewBuildUp<IUnityContainer>();
        List<T> results = new List<T>(container.ResolveAll<T>());
        return results.ToArray();
    }

The current set of overrides is part of the current build context, not the container itself, so when it grabs the container and reresolves that context is lost.

Darn, I'll have to figure out how to fix this one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文