不能从收集转换为Microsoft.Practices.unity.injectionmember
问题描述:
我使用依赖注入等在配置文件中团结包括我不能从收集<Interface>转换为Microsoft.Practices.unity.injectionmember
container.RegisterType<ICollection<IModifObserver>>(new Collection<IModifObserver>() { new NotifyHisto() });
,但我有转换的错误从无法从collection<IModifObserver>
转换为Microsoft.Practices.unity.injectionmember
我想用一个对象的实例来填充我的集合。
答
如果你想注册一个实例,使用RegisterInstance
方法是这样的:
container
.RegisterInstance<ICollection<IModifObserver>>(
new Collection<IModifObserver>() { new NotifyHisto() });
如果你希望每次解决时间的容器返回一个新的实例,那么你可以使用InjectionFactory
类的RegisterType
这样的方法:
container
.RegisterType<ICollection<IModifObserver>>(
new InjectionFactory(
c => new Collection<IModifObserver>() { new NotifyHisto() }));