所以我决定将我的SimpleInjector版本升级到3.0,突然我收到一条消息:
“simpleInjector.extensions.openGenericBatchRegistrationExtensions.registerManyForOpenGeneric(simpleInjector.container,system.type,params system.reflection.assembly[])”已过时:“此扩展方法已被删除。请使用容器。请改为注册(类型,IEnumerable)。
文档中仍有此方法:
http://simpleinjector.readthedocs.org/en/latest/advanced.html
所以我很好奇,除了:
container.RegisterManyForOpenGeneric(typeof(IEventHandler<>),
container.RegisterAll,
typeof(IEventHandler<>).Assembly);
最佳答案
啊..我搔了几个小时的头,才发现:
container.RegisterCollection(typeof(IEventHandler<>),
typeof(IEventHandler<>).Assembly);
RegisterCollection
还处理打开的泛型。也许这应该记录在某个地方。编辑:
我在新文档中意识到,上述代码不是registermanyforopengeneric的直接翻译。它只解决了我的编译问题,但它没有注册我的处理程序,我只是今天检查了一下。
Additional information: No registration for type
这是正确的版本:
container.Register(typeof(IEventHandler<>),
new[] { typeof(IEventHandler<>).Assembly });
使用registercollection将需要一些额外的代码更改(来自文档):
因为我们注册了一个集合,所以不能再调用container.getInstance>()相反,可以通过使用IEnumerable>构造函数参数或通过调用container.getAllInstances>(),来检索实例。
我没有做过,也不需要做,因为我没有混合开放泛型和非泛型。但如果我想修改我的项目的话,我会在未来对此进行更多的探索。