将IsClosedTypeOf从autofac转换为simple.injector



我正在关注.net junkie的CQS帖子,并实现了QueryProcessor。在这篇文章中,他引用了:-

使用IQueryProcessor意味着我们必须编写一个测试来确认对于系统中的每个查询都有相应的查询处理程序,因为DI框架无法为您检查这一点。

在这篇文章的评论中,有人使用autofac创建了一个使用扩展方法IsClosedTypeOf(typeof(IQuery<>))的测试。我想使用简单的注入器

然而,我正在努力解决如何在不使用auto-fac的情况下转换/创建扩展方法IsClosedTypeOf

    var allQueryTypes = Assembly.GetAssembly(typeof(IQuery<>)).GetTypes()
            .Where(t => t.IsClass && t.IsClosedTypeOf(typeof(IQuery<>)))
            .ToList();

我甚至不能100%确定IsClosedTypeOf的功能,因为我不熟悉autofac

测试代码的来源。

var allQueryTypes =
    from assembly in AppDomain.CurrentDomain.GetAssemblies()
    from type in assembly.GetTypes()
    where !type.IsAbstract && !type.IsGenericTypeDefinition
    let queryInterfaces =
        from iface in type.GetInterfaces()
        where iface.IsGenericType
        where iface.GetGenericTypeDefinition() == typeof(IQuery<>)
        select iface
    where queryInterfaces.Any()
    select type;

相关内容

最新更新