Autofac构造、属性、方法等注入 本文共有8981个字,关键词: 废话不多,直接上代码 public interface IMainClass { void MainFunc(); void TestFunc(IChildClass childClass); } public interface IChildClass { void fun1(); } public class ChildClass : IChildClass { public ChildClass() { Console.WriteLine(this.GetType() + "-----"); } public void fun1() { Console.WriteLine(this.GetType() + "--fun1"); //to do sth } } public interface IChildClass2 { void fun2(); } public class ChildClass2 : IChildClass2 { public ChildClass2() { Console.WriteLine(this.GetType() + "-----"); } public void fun2() { //to do sth } } public class MainClass : IMainClass { /// <summary> /// 需要注入的属性 /// </summary> [AutofacSelectAttribute] public IChildClass childClass1 { get; set; } public IChildClass childClass2 { get; set; } private readonly IChildClass _ichildClass; private readonly IChildClass2 _ichildClass2; /// <summary> /// 1个参数的构造函数 /// </summary> /// <param name="childClass"></param> public MainClass(IChildClass childClass) { Console.WriteLine(this.GetType() + "1个参数构造"); _ichildClass = childClass; } /// <summary> /// 2个参数的构造函数 /// </summary> /// <param name="childClass"></param> /// <param name="childClass2"></param> public MainClass(IChildClass childClass, IChildClass2 childClass2) { Console.WriteLine(this.GetType() + "2个参数构造"); _ichildClass = childClass; _ichildClass2 = childClass2; } public void MainFunc() { //to do main func _ichildClass.fun1(); } IChildClass TestchildClass1; /// <summary> /// 测试注入的方法 /// </summary> /// <param name="childClass"></param> public void TestFunc(IChildClass childClass) { TestchildClass1 = childClass; TestchildClass1.fun1(); } } public class MainClass2 : IMainClass { public void MainFunc() { Console.WriteLine("here is a new MainClass"); } public void TestFunc(IChildClass childClass) { } }using Autofac; ContainerBuilder containerBuilder = new ContainerBuilder(); containerBuilder.RegisterType<ChildClass>().As<IChildClass>(); containerBuilder.RegisterType<ChildClass2>().As<IChildClass2>(); containerBuilder.RegisterType<MainClass>().As<IMainClass>();//默认注册构造函数多的 //containerBuilder.RegisterType<MainClass>().UsingConstructor(typeof(IChildClass)) .As<IMainClass>();//注册构造函数为1个的 //containerBuilder.RegisterType<MainClass>().As<IMainClass>().PropertiesAutowired(); //支持全部属性注入 //containerBuilder.RegisterType<MainClass>().As<IMainClass>(). PropertiesAutowired(new AutofacPropertySelector());//支持部份标记属性注入 //containerBuilder.RegisterType<MainClass>().As<IMainClass>().OnActivated(activate => //{ // IChildClass childClass = activate.Context.Resolve<IChildClass>(); // activate.Instance.TestFunc(childClass); //});//方法注入 //使用Keyed以注册2个或2个以上实现 containerBuilder.RegisterType<MainClass>().Keyed<IMainClass>("Main1"); containerBuilder.RegisterType<MainClass2>().Keyed<IMainClass>("Main2"); IContainer container = containerBuilder.Build(); IMainClass mainClass = container.Resolve<IMainClass>(); //获取IMainClass的列表 IEnumerable<IMainClass> lstmainClass = container.Resolve<IEnumerable<IMainClass>>(); // or IMainClass mainClass = container.ResolveKeyed<IMainClass>("Main1"); IMainClass mainClass2 = container.ResolveKeyed<IMainClass>("Main2"); mainClass.MainFunc(); mainClass2.MainFunc(); Console.WriteLine("-------------------------------------"); Console.ReadLine(); 「一键投喂 软糖/蛋糕/布丁/牛奶/冰阔乐!」 赞赏 × 梦白沙 (๑>ڡ<)☆谢谢老板~ 1元 2元 5元 10元 50元 任意金额 2元 使用微信扫描二维码完成支付 版权声明:本文为作者原创,如需转载须联系作者本人同意,未经作者本人同意不得擅自转载。 dotNET 2022-05-17 评论 272 次浏览