.net - How to call generic methods with generic type parameter -


its more generics ninject, curious.

the following is working fine.

kernel.bind(typeof(ientityrepository<,>)).to(typeof(loggerrepository<,>)); 

but if want use generics? following gives me compile time error.

kernel.bind<ientityrepository<,>>().to<loggerrepository<ientity<>,int>(); 

or

kernel.bind<ientityrepository<,>>().to<loggerrepository<,>(); 

i sure missing pretty simple, , must have got answered in st. can kindly direct me answer please?

edit: following works fine.

kernel.bind<ientityrepository<appuser, int>>().to<entityrepository<appuser, int>>(); 

but guess there should way without specifying types(appuse , int).

when not type arguments of generic specified, cannot used in expression other typeof(). article helpful you: unbound generics: open , closed case

i'm referring part in particular, discusses use of unbound generics in conjunction dependency injection:

when use code perform registration, omit type parameters; example registertype(typeof(mytypes.imyinterface<,>)). saw earlier, microsoft says "the typeof operator can operate on unbound generic types (generic types not yet have specific type arguments)"

edit:

generally speaking, using unbound generics dependency injection lead confusing , hard-to-read code. if i'm not mistaken trying way because of constraints can put on type arguments.

but why not define parent interface types use? sure, you'll lose constraints, necessary in situation this? controlling binding on 1 location, di container. constraints used when generic types exposed through api others use. code not wired through di container client anyway, because either use derived classes, factories, or instantiate directly.

is there particular reason why want way?

edit #2:

perhaps you're looking this?

kernel.bind(typeof(irepository<>)).to(typeof(repository<>)); 

then, when have irepository<someentity> constructor parameter in calling code, ninject resolve repository<someentity> @ run-time.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -