0

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.
Cel
  • 6,467
  • 8
  • 75
  • 110
  • Here are two ways to do it: http://blog.ploeh.dk/2012/07/02/PrimitiveDependencies – Mark Seemann Mar 04 '14 at 16:46
  • See also http://blog.ploeh.dk/2012/11/07/AppSettingsconventionforCastleWindsor – Mark Seemann Mar 04 '14 at 16:46
  • @MarkSeemann Thanks but which two? I had a quick glance and I'm not sure how to apply it to the current situation given I am talking about arguments provided at resolution-time from code to an injected factory (with the CreatePercentage method) and not arguments provided at composition root; And i mean which two given the need to not use hard-coded string names as that does not work with obfuscation :( – Cel Mar 04 '14 at 16:56
  • Ah, sorry, I misread your question. Given your constraints (no string literals), I'm not sure it's possible: http://docs.castleproject.org/Windsor.Passing-Arguments.ashx – Mark Seemann Mar 04 '14 at 17:21

1 Answers1

2

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.

Aleš Roubíček
  • 5,198
  • 27
  • 29
  • Thanks, it does work! While `new Arguments(part, total)` does not as it is using another overload, which is dictionary-based I believe – Cel Mar 05 '14 at 08:39
  • After testing it with obfuscation, turns out this approach does not work - it still relies on the parameter variable names, which get changed on obfuscation ... – Cel Mar 09 '14 at 20:16