我有以下
class GetData : Query
class Data : Result
class IHandler<TIn,TOut>
class IQueryHandler<TQuery,TResult> : IHandler<TQuery, IEnumerable<TResult>>
class DefaultQueryHandler<TQuery,TResult> : IQueryHander<TQuery,TResult>
Container.RegisterConditional(
typeof(IHandler<,>),
typeof(DefaultQueryHandler<,>),
c => c!.Handled
)
我已按照此处的指南https://simpleinjector.readthedocs.io/en/latest/advanced.html#registration-of-open-generic-types认为
Container.GetInstance<IHandler<GetData,IEnumerable<Data>>>
应该实例化DefaultQueryHandler<GetData, Data>
但是我没有找到IHandler&#39;
的注册谢谢
答案 0 :(得分:0)
我尝试重现您的错误,但是当我在控制台应用程序中运行它时,一切都按预期工作。这是我用来重现的代码:
using System;
using System.Collections.Generic;
using SimpleInjector;
class Query { }
class Result { }
class GetData : Query { }
class Data : Result { }
class IHandler<TIn, TOut> { }
class IQueryHandler<TQuery, TResult> : IHandler<TQuery, IEnumerable<TResult>> { }
class DefaultQueryHandler<TQuery, TResult> : IQueryHandler<TQuery, TResult> { }
class Program
{
static void Main(string[] args)
{
var container = new Container();
container.RegisterConditional(
typeof(IHandler<,>),
typeof(DefaultQueryHandler<,>),
c => !c.Handled);
var handler = container.GetInstance<IHandler<GetData, IEnumerable<Data>>>();
Console.WriteLine(handler.GetType().FullName);
Console.ReadLine();
}
}
GetInstance
调用返回以下关闭类型:
DefaultQueryHandler<GetData, Data>