如何统一解决一个接口的所有实现?

时间:2014-08-31 17:42:24

标签: c# unity-container

好的,我想创建特定接口的任意实现并解决它们。它实际上与插件系统类似。 “创建自己的interface 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?


当我检查我的容器注册时,我的实现是在那里,名称是的名称 - 而不是接口。精彩。 但为什么我无法解决IMenuEntry的所有实现?它总是空的。

1 个答案:

答案 0 :(得分:3)

我正在查看此post,听起来WithMappings.FromMatchingInterface只会将名为MenuEntry的类型与IMenuEntry匹配。

您可以尝试使用WithMappings.FromAllInterfaces