温莎城堡中相同类型的解析时间参数 [英] Resolution-time arguments of same type in Castle Windsor

查看:92
本文介绍了温莎城堡中相同类型的解析时间参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试传递两个相同类型的参数时,如下所示:

When I try to pass two parameters that are of the same type like so:

public IPercentage CreatePercentage(int part, int total)
{
    return _container.Resolve<T>(new Arguments(part, total));
}

对于这样的构造函数:

public Percentage(int part, int total)
{
   // ...
}

然后我得到一个 System.ArgumentException:已经添加了具有相同键的项。

如何传递相同类型的参数?

How can I pass arguments of same type?


  • 关键是我想为了避免使用参数的文字字符串名称来标识哪些参数在哪里

  • ,而是使用参数的顺序

  • 以及它是唯一适合的构造函数,尽管我猜想Windsor的字典实现不允许这样做。

推荐答案

绝对可行,根据文档正确调用 是:

It's absolutely doable, correct call according to documentation is:

_container.Resolve<IPercentage>(new Arguments(new { part, total }));

但是首选方法是使用 TypedFactoryFacility 。除了入口点和/或组合根以外,永远不要从代码中调用容器。

But the preferred way is to use the TypedFactoryFacility. You should never call container from your code except the entry point and/or composition root.

这篇关于温莎城堡中相同类型的解析时间参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆