温莎城堡中的 Pick().If() 与 Where()

Pick().If() vs Where() in Windsor Castle

温莎城堡中函数 if() 和 where() 有什么区别。 我在代码中遇到了这两种组件注册方式

1)

container.Register(
            Classes.FromAssemblyInThisApplication()
                .Where(x=> x.IsClass && x.GetAllInterfaces().Length > 0)
                .WithService.AllInterfaces()
                .LifestyleSingleton());

2)

container.Register(
            Classes.FromAssemblyInThisApplication()
                .Pick()
                .If(x=> x.IsClass && x.GetAllInterfaces().Length > 0)
                .WithService.AllInterfaces()
                .LifestyleSingleton());

请问这两种组件注册方式有什么区别

如果您查看源代码,您会发现 Where 做(实际上)与 Pick() 相同的事情,另外在内部添加了对 .If 的调用。这是查看代码的内部工作原理,因此不能保证这不会在某些时候因任何原因而改变。

Where:

public BasedOnDescriptor Where(Predicate<Type> accepted)
{
    var descriptor = new BasedOnDescriptor(typeof(object), this, additionalFilters).If(accepted);
    criterias.Add(descriptor);
    return descriptor;
}

Pick

public BasedOnDescriptor Pick()
{
    return BasedOn<object>();
}

public BasedOnDescriptor BasedOn<T>()
{
    return BasedOn(typeof(T));
}

public BasedOnDescriptor BasedOn(Type basedOn)
{
    var descriptor = new BasedOnDescriptor(basedOn, this, additionalFilters);
    criterias.Add(descriptor);
    return descriptor;
}