[英]How to resolve all implementations of one interface in unity?
Okay, I want to create arbitrary implementations of a specific Interface and resolve them. 好的,我想创建特定接口的任意实现并解决它们。 It's actually similiar to a plugin system. 它实际上与插件系统类似。 "Create your own Implementation of intereface X and the app does the rest" “创建自己的intereface X实现,应用程序完成其余的工作”
public interface IMenuEntry { ... }
// auto-mapping (registration-by-convention)
_container.RegisterTypes(AllClasses.FromAssemblies(assemblies),
WithMappings.FromMatchingInterface,
// ResolveAll need names for multiple registrations of the same type (?)
WithName.TypeName,
WithLifetime.ContainerControlled);
var g = _container.ResolveAll<IMenuEntry>(); // empty - why?
When I check my container registrations my implemantations are there and the name is the name of the class - not the interface. 当我检查我的容器注册时,我的实现就在那里,名称是类的名称 - 而不是接口。 Wonderful. 精彩。 But why can't I resolve all implementations of IMenuEntry
? 但为什么我不能解决IMenuEntry
所有实现? It is always empty. 它总是空的。
I'm looking at this post , and it sounds like WithMappings.FromMatchingInterface
will only match a type named MenuEntry
to IMenuEntry
. 我正在看这篇文章 ,听起来像WithMappings.FromMatchingInterface
只会将名为MenuEntry
的类型与IMenuEntry
。
You might try WithMappings.FromAllInterfaces
instead. 您可以尝试使用WithMappings.FromAllInterfaces
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.