当使用城堡windsor进行自动注册时,我看到有人在做像
这样的事情_container.Register(
AllTypes.Pick().FromAssembly(Assembly.GetExecutingAssembly())
.WithService.FirstInterface());
对于我的生活,我无法弄清楚Pick()方法的作用,也无法找到任何文档。任何人都可以向我解释一下吗?
答案 0 :(得分:9)
Pick(IEnumerable<Type>)
is a synonym for From(IEnumerable<Type>)
,即选择指定类型作为注册目标。
AllTypes.Pick()
is the same as AllTypes.Of<object>()
,因此它有效地选择了所有类型。
AllTypes.Pick().FromAssembly(Assembly.GetExecutingAssembly())
将在执行程序集中选择所有类型(当然可以过滤)
与往常一样,请查看fluent API wiki和/或test case了解详情。
答案 1 :(得分:2)
这是流畅的API中的起点,用于选择哪些类型将自动注册到容器中。
Container.Register(
AllTypes.Pick()
.FromAssemblyNamed("MyAssembly")
.If(t => t.Name.EndsWith("ABC"))
.Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
.WithService.Select(i => typeof(I))
);
在此示例中,从名称以“ABC”结尾的MyAssembly中挑选的所有类型都将添加到具有Transient生活方式作为类型I的服务的容器中。示例来自this question。
这是一种内部DSL形式的声明式方法。使用这种API,方法用于排序稍后将执行的行为。为了实现这一点,这些方法返回构建器指导配置步骤,而实际工作则在最后完成。