1

注意:在我开始之前,请注意这在 ICommandHandler 的定义被更改为包含多个通用约束之前完美运行,一个约束它工作正常。

但是,我似乎没有将多个约束传递到选择器的“参数”数组中。我错过了什么吗?

这被称为:

   var handler = _factory.GetHandlerForCommand<TCommand, TResult>(command);

工厂界面:

    public interface ICommandHandlerFactory
    {
        ICommandHandler<TCommand, TResult> GetHandlerForCommand<TCommand, TResult>(ICommand command) 
            where TCommand : class, ICommand 
            where TResult : IDTOBase;
    }

选择器类:

    public class HandlerSelector : DefaultTypedFactoryComponentSelector 
    {
        protected override Func<Castle.MicroKernel.IKernelInternal, Castle.MicroKernel.IReleasePolicy, object> BuildFactoryComponent(System.Reflection.MethodInfo method, string componentName, Type componentType, System.Collections.IDictionary additionalArguments)
        {
            return new HandlerResolver(componentName,
                                                    componentType,
                                                    additionalArguments,
                                                    FallbackToResolveByTypeIfNameNotFound,
                                                    GetType()).Resolve;
        }

        protected override string GetComponentName(System.Reflection.MethodInfo method, object[] arguments)
        {
            return null;
        }

        protected override Type GetComponentType(System.Reflection.MethodInfo method, object[] arguments)
        {
            var message = arguments[0];
            var handlerType = typeof (ICommandHandler<,>).MakeGenericType(message.GetType());
            return handlerType;
        }
    }

Windsor 安装程序文件:

           container
                .Register(
                    Component
                        .For<HandlerSelector>()
                        .ImplementedBy<HandlerSelector>(),
                    AllTypes
                        .FromAssemblyContaining<ICommandHandlerFactory>()
                            .BasedOn(typeof(ICommandHandler<,>))
                            .WithService.Base()
                            .Configure(c => c.LifeStyle.Is(LifestyleType.PerWebRequest)),
                    Component
                        .For<ICommandHandlerFactory>()


                .AsFactory(c => c.SelectedWith<HandlerSelector>()));
4

1 回答 1

2

通常情况下,以连贯的方式编写问题通常会导致答案。

将选择器代码更改为:

var genericArgs = method.GetGenericArguments();
var handlerType = typeof(ICommandHandler<,>).MakeGenericType(genericArgs[0], genericArgs[1]);
return handlerType;

解决问题。

于 2012-05-29T15:37:33.993 回答