Pregunta

I want to bind IDicitionary<> to Dictionary<>. I tried the following binding:

Bind(typeof (IDictionary<,>)).To(typeof (Dictionary<,>));

Ninject sees the copy constructor, and tries to use it - which leads to a cyclic dependency.

How do I correctly specify this binding?

¿Fue útil?

Solución

One workaround and two solutions i can think of (pick the one which suits you best):

  • Create your own Dictionary MyDictionary<,> : Dictionary<,> (with only a single parameterless ctor) and Bind(typeof (IDictionary<,>)).To(typeof (MyDictionary<,>));

  • Adapt the binding to: this.Bind(typeof(IDictionary<,>)).ToMethod(ctx => Activator.CreateInstance(typeof(Dictionary<,>).MakeGenericType(ctx.GenericArguments)));

  • Adapt the binding to this.Bind(typeof(IDictionary<,>)).ToConstructor(ctx => Activator.CreateInstance(typeof(Dictionary<,>).MakeGenericType(ctx.Context.GenericArguments)));

For the difference between .ToMethod()and .ToConstructor() see: What's the difference between .ToConstructor and .ToMethod in Ninject 3?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top