Ninject: How to bind an open generic with more tha

2019-03-10 20:37发布

问题:

I'm using Ninject 2.2, and I'm trying to setup a binding for an open generic that takes two type arguments. According to this answer by qes, the correct syntax to bind IRepository<T> to Repository<T> is this:

Bind(typeof(IRepository<>)).To(typeof(Repository<>));

The above syntax works perfectly if IRepository takes just one type argument, but breaks if it takes more (gives a Using the generic type 'Repository<T,U>' requires 2 type arguments compile time error.)

How can I bind IRepository<T,U> to Repository<T,U>?

Thanks.

回答1:

Bind(typeof(IRepository<,>)).To(typeof(Repository<,>));

Try that....



标签: c# ninject-2