Unity: Register and resolve class with generic type

岁酱吖の 提交于 2019-11-30 20:30:39

T may be any type. I want to be able to create instances of ICollectionWrapper without having to register every possible combination of T.

That's what register generics is for. Some IOC name the method as RegisterGeneric to make it self explanatory (autofac for example), but unity keep it just an overload of RegisterType.

container.RegisterType(typeof(ICollectionWrapper<>), typeof(CollectionWrapper<>), new TransientLifetimeManager());

Also note that your injectable is having multiple constructors. That itself is considered as anti-pattern.

If you fix the multiple construtor thing, above registration will work.

Debendra Dash
public static void RegisterTypes(IUnityContainer container)
{
   container.RegisterType(typeof(IEmployee<>), typeof(Employee<>), new TransientLifetimeManager());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!