2010-10-19 144 views
1

我有一个接口,它定义了一些方法,我有N个类来实现它。我如何注册所有加载的程序集中的所有类与autofac?Autofac - 注册插件

回答

1

您将不得不“知道”包含这些类的程序集,您可以使用Assembly.Load(..)自己加载它们。

从那里,很容易注册类:

var assemblies = new[]{....}; 

builder.RegisterAssemblyTypes(assemblies) 
      .Where(t => typeof(IMyInterface).IsAssignableFrom(t)) 
      .As<IMyInterface>(); 

更新:去注册的情况下,你可以使用Autofac内置支持类别:

public class MyService 
{ 
    private readonly IEnumerable<IMyInterface> _services; 
    public MyService(IEnumerable<IMyInterface> services) 
    { 
     _services = services; 
    } 

    public void DoStuffWithServices() 
    { 
     foreach(var svc in _services) 
     { 
      ... 
     } 
    } 
} 
+0

你能给我写一篇关于如何编写可扩展调试类的例子,我可以用autofac注册dinamically?我应该使用模块吗?为什么? – Stefano 2010-10-21 18:18:36

+0

@Stefano - 严肃地说,这个问题根本就没有意义。它与这个线程有关吗?如果不是,请创建一个新问题并尝试解释您的问题。如果没有可以理解的问题/场景,我不能也不会给你“codez”。 – 2010-10-21 19:40:13

+0

所以,它真的很简单...我有N个类实现相同的接口,这些类在不同的程序集...所以我注册了所有与您的方法找到的类,但我不知道类的名称,所以如何我能给他们打电话吗? – Stefano 2010-10-23 16:57:51