Autofac扫描组件为某些类的类型 [英] Autofac Scanning Assemblies for certain class type

查看:103
本文介绍了Autofac扫描组件为某些类的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Autofac和要扫描的一些DLL的,并得到Autofac以在其中注册一些类的开始。

这是我感兴趣的所有类从PluginBase类继承,但低于code似乎并没有被registerting他们。任何人都可以帮忙吗?

  VAR组件= AppDomain.CurrentDomain.GetAssemblies();


        VAR建设者=新ContainerBuilder();
        builder.RegisterAssemblyTypes(组件)
            。凡(T => t.BaseType == typeof运算(PluginBase))
            .AsImplementedInterfaces()
            .AsSelf();

        变种容器= builder.Build();
        VAR pluginClasses = container.Resolve< IEnumerable的< PluginBase>>();

        // pluginClasses是空!!!!
 

解决方案

我想你需要指定插件上注册的基类。呼叫 AsImplementedInterfaces 注册其实现的接口,而不是由它的基类型的类型。您应该更新您的注册登记你的插件,PluginBase。

Here's的code:

  VAR组件= AppDomain.CurrentDomain.GetAssemblies();


    VAR建设者=新ContainerBuilder();
    builder.RegisterAssemblyTypes(组件)
        。凡(T => t.BaseType == typeof运算(PluginBase))
        。随着< PluginBase>();

    变种容器= builder.Build();
    VAR pluginClasses = container.Resolve< IEnumerable的< PluginBase>>();
 

I've started using Autofac and want to scan some DLL's and get Autofac to register some of the classes within them.

The classes that I'm interested in all inherit from a PluginBase class but the below code doesn't seem to be registerting them. Can anyone help?

        var assemblies = AppDomain.CurrentDomain.GetAssemblies();


        var builder = new ContainerBuilder();
        builder.RegisterAssemblyTypes(assemblies)
            .Where(t => t.BaseType == typeof(PluginBase))
            .AsImplementedInterfaces()
            .AsSelf();

        var container = builder.Build();
        var pluginClasses = container.Resolve<IEnumerable<PluginBase>>();

        //pluginClasses is empty!!!!

解决方案

I think you need to specify the base class of your Plugins on registration. The call AsImplementedInterfaces registers the type with its implemented interfaces and not by its base type. You should update your registration to register your plugins as PluginBase.

Here´s the code:

var assemblies = AppDomain.CurrentDomain.GetAssemblies();


    var builder = new ContainerBuilder();
    builder.RegisterAssemblyTypes(assemblies)
        .Where(t => t.BaseType == typeof(PluginBase))
        .As<PluginBase>();

    var container = builder.Build();
    var pluginClasses = container.Resolve<IEnumerable<PluginBase>>();

这篇关于Autofac扫描组件为某些类的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆