3

I have a similar setup shown in this question and was wondering if I could replace all my code that uses reflection to find plugins of a certain type and then does a CreateInstance on it with a IOC Container that does something like:

builder.RegisterAssemblyTypes(typeof(MyType).Assembly)
Community
  • 1
  • 1
Jon
  • 38,814
  • 81
  • 233
  • 382

1 Answers1

1

Yes, you could do something like this:

builder.RegisterAssemblyTypes(typeof(MyType).Assembly).AsImplementedInterfaces();
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • What does the AsImplementedInterfaces do? The project has legacy code so will not have constructors with interfaces and will implement some interfaces concretely. – Jon Feb 05 '12 at 19:32
  • From the documentation of Autofac: *Specifies that a type from a scanned assembly is registered as providing all of its implemented interfaces.* – Mark Seemann Feb 05 '12 at 20:10
  • What if the class doesn't have interface constructors will they just be ignored? Just trying to minimalize impact! – Jon Feb 05 '12 at 20:19
  • @Jon - you could, in addition to `AsImplementedInterfaces`, add `.AsSelf()`. That way, the container will be able to resolve services both as any interface they implement as well as their concrete classes. – Peter Lillevold Feb 05 '12 at 22:27