Resolution-time arguments of same type in Castle Windsor

≡放荡痞女 提交于 2019-12-11 19:07:18

问题


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));
}

To a constructor like so:

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

Then I get a System.ArgumentException: An item with the same key has already been added.

How can I pass arguments of same type?

  • The key thing is I would like to avoid using literal string names of the parameters to identify which arguments goes where
  • And instead use the order of the arguments
  • and just the fact that it is the only constructor that fits, although I'm guessing the dictionary implementation of Windsor does not allow that.

回答1:


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

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

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.



来源:https://stackoverflow.com/questions/22176965/resolution-time-arguments-of-same-type-in-castle-windsor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!