CollectionResolver
More generic alternative to ArrayResolver and ListResolver.
It supports arrays as well as any generic interface type assignable from arrays.
using Castle.Core;
using Castle.Core.Internal;
using Castle.MicroKernel.Context;
using System;
namespace Castle.MicroKernel.Resolvers.SpecializedResolvers
{
public class CollectionResolver : ISubDependencyResolver
{
private readonly bool allowEmptyCollections;
private readonly IKernel kernel;
public CollectionResolver(IKernel kernel, bool allowEmptyCollections = false)
{
this.kernel = kernel;
this.allowEmptyCollections = allowEmptyCollections;
}
public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
{
if (dependency.TargetItemType == (Type)null)
return false;
Type compatibleArrayItemType = dependency.TargetItemType.GetCompatibleArrayItemType();
if (compatibleArrayItemType == (Type)null)
return false;
if (!allowEmptyCollections)
return kernel.HasComponent(compatibleArrayItemType);
return true;
}
public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
{
return kernel.ResolveAll(dependency.TargetItemType.GetCompatibleArrayItemType(), null);
}
}
}