Autofac-注册所有Windows窗体 [英] Autofac - Register all Windows Forms

查看:53
本文介绍了Autofac-注册所有Windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以要求Autofac在我的程序集中自动注册所有Windows窗体?此功能很可能类似于Autofac MVC支持的注册所有控制器的功能吗?

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?

现在,我知道了

foreach (var type in Assembly.GetExecutingAssembly().GetTypesSafely().Where(type => type.IsSubclassOf(typeof(Form))))
    builder.RegisterType(type);

推荐答案

据我所知,没有内置的方法可以添加所有Win窗体.但是你可以做这样的事情.

As far as I know there isn't a built in method to add all Win Forms. But you can do something like this.

var assembly = Assembly.GetExecutingAssembly();
builder.RegisterAssemblyTypes(assembly)
    .Where(type => type.IsSubclassOf(typeof(Form)));

这篇关于Autofac-注册所有Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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