Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofac - Register all Windows Forms

Is there a way to ask Autofac to automatically register all Windows Forms in my assembly? This feature would most likely be similar to the Autofac MVC support's ability to register all controllers?

Right now, I do:

foreach (var type in Assembly.GetExecutingAssembly().GetTypesSafely().Where(type => type.IsSubclassOf(typeof(Form))))
    builder.RegisterType(type);
like image 759
David Pfeffer Avatar asked Dec 22 '22 12:12

David Pfeffer


1 Answers

Here's a short and sweet way of doing it:

var assembly = Assembly.GetExecutingAssembly();
builder.RegisterAssemblyTypes(assembly)
    .AssignableTo<Form>();
like image 175
Peter Lillevold Avatar answered Jan 03 '23 05:01

Peter Lillevold