using Autofac; using System.Reflection; using WcsMain.WcsAttribute.AutoFacAttribute; namespace WcsMain.StartAction; public class AutofacModule : Autofac.Module { /* * 此处为依赖注入,注入的是单例模式 */ protected override void Load(ContainerBuilder builder) { var assembly = Assembly.GetExecutingAssembly(); // 注册 Service builder.RegisterAssemblyTypes(assembly) .Where(w => w.GetCustomAttribute(typeof(ServiceAttribute)) != default) .SingleInstance(); // 注册 Component builder.RegisterAssemblyTypes(assembly) .Where(w => w.GetCustomAttribute(typeof(ComponentAttribute)) != default) .SingleInstance(); // 添加属性注入 --- 一般不建议使用 builder.RegisterAssemblyTypes(assembly) .Where(w => w.GetCustomAttribute(typeof(AutowiredAttribute)) != default) .PropertiesAutowired(new AutowiredSelector()); } }