问题
I have an interface ISomething with a method Start. I want to get all implementations of this interface (in multiple assemblies, the main one and all referenced ones) and call the Start method on application start. How can I do this with Autofac 2.4.4.705?
回答1:
you can resolve
IEnumerable<ISomething>
and call Start for each of them
Forgot to mention, that you should first register all implementations of ISomething.
Assembly[] assemblies = ...;
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(assemblies).AssignableTo<ISomething>().As<ISomething>();
var container = builder.Build();
Where "assemblies" is an array of assemblies you want to register from.
来源:https://stackoverflow.com/questions/5074719/calling-all-isomething-instances-in-autofac