Skip to content

Instantly share code, notes, and snippets.

@NeilRobbins
Created October 11, 2010 12:21
Show Gist options
  • Save NeilRobbins/620434 to your computer and use it in GitHub Desktop.
Save NeilRobbins/620434 to your computer and use it in GitHub Desktop.
// version 1.0.3.0 -- Don't ask, I know... :(
// I want to pass in an array of enum values to a constructor of a type.
// The code in my config looks like the below
// The error I get now is
// ArgumentException "An item with the same key has already been added."
// Stack trace (cleaned) is:
// at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
// at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
// at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
// at Castle.Core.ParameterModelCollection.Add(String name, IConfiguration configNode)
// at Castle.MicroKernel.ModelBuilder.Inspectors.ConfigurationParametersInspector.ProcessModel(IKernel kernel, ComponentModel model)
// at Castle.MicroKernel.ModelBuilder.DefaultComponentModelBuilder.BuildModel(String key, Type service, Type classType, IDictionary extendedProperties)
// at Castle.MicroKernel.Registration.ComponentRegistration`1.Castle.MicroKernel.Registration.IRegistration.Register(IKernel kernel)
// at Castle.MicroKernel.DefaultKernel.Register(IRegistration[] registrations)
// at Castle.Windsor.WindsorContainer.Register(IRegistration[] registrations)
// at Module.Configure(IWindsorContainer windsorContainer) in C:\svn\App\Config\Module.cs:line 112
// at Company.CastleConfig.InstallModules() in C:\App\Windsor\Config\CastleConfig.cs:line 28
// The code I'm using is this
windsorContainer.Register(Component.For<IDoSomething>()
.ImplementedBy<SpecificDoer>()
.Configuration(Child.ForName("parameters").Eq(
Child.ForName(
"statuses")
.Eq(
Child.ForName("array").Eq(
Attrib.ForName("item").Eq("FOO"),
Attrib.ForName("item").Eq("BAR"),
Attrib.ForName("item").Eq("BAZ")
)
)
)));
// where SpecificDoer has a single ctor with the signature
public SpecificDoer(IWantADependency thanks, IWantAnotherDependency thanksAgain, StatusCode[] statuses)
{
// the various assignments
}
// and where StatusCode is an enum like this:
public enum StatusCode
{
FOO,
BAR,
BAZ,
FIZZ,
BUZZ
}
@BenHall
Copy link

BenHall commented Oct 11, 2010

Have you looked at : http://mikehadlow.blogspot.com/2008/09/resolving-arrays-with-windsor.html

Resolving arrays required an additional Facility as it doesn't come out of the box like StructureMap

EDIT: Sorry SubResolver - not Facility (kernel.Resolver.AddSubResolver(new ArrayResolver(kernel));)

@kkozmic
Copy link

kkozmic commented Oct 11, 2010

You need to wrap the "statuses" in "parameters" for this to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment