【问题标题】:Resolving interface with generic type using Castle Windsor使用 Castle Windsor 解析具有泛型类型的接口
【发布时间】:2012-08-30 04:00:45
【问题描述】:

我正在尝试使用具有继承自的泛型类型的接口来解析具体类型。

这一切似乎都有效,但是当我尝试在已解析的具体类型上调用方法时,它会引发以下异常:

{"最佳重载方法匹配 'MvcApplication2.Utilities.Common.CQRS.GetAlbumsByIdQueryHandler.Execute(MvcApplication2.Utilities.Common.CQRS.GetAlbumsByIdQuery)' 有一些无效的参数”}

有人知道是什么原因造成的吗?解析的类型是正确的,我觉得可能和使用动态类型有关?

谢谢。


这是我的处理程序安装程序:

container.Register(AllTypes.FromAssemblyContaining<IQueryHandler>()
                                        .BasedOn(typeof (IQueryHandler<>))
                                        .WithService
                                        .AllInterfaces()
                                        .LifestyleSingleton());

目前的分辨率是:

        Type queryHandlerType = typeof (IQueryHandler<>).MakeGenericType(query.GetType());
        dynamic queryHandler = _kernal.Resolve(queryHandlerType);

        return queryHandler.Handle(query);

以及相关的类:

public interface IQueryHandler<in TQuery> : IQueryHandler where TQuery : IQuery
{
    IQueryResult Handle(TQuery query);
}


public class GetAlbumsByIdQueryHandler : IQueryHandler<GetAlbumsByIdQuery>
{
    #region IQueryHandler<GetAlbumsByIdQuery> Members

    public IQueryResult Execute(GetAlbumsByIdQuery query)
    {
        throw new NotImplementedException();
    }

    #endregion
}

【问题讨论】:

    标签: c# castle-windsor


    【解决方案1】:
      return queryHandler.Handle((dynamic)query);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多