目录

  1. 开始
  2. Registering components
  3. 控制范围和生命周期
  4. 用模块结构化Autofac
  5. xml配置
  6. 与.net集成
  7. 深入理解Autofac
  8. 指导
  9. 关于
  10. 词汇表

属性注入使用可写属性而不是构造函数参数实现注入。

介绍

如果component是一个委托,使用一个对象初始化:

1
builder.Register(c => new A { B = c.Resolve<B>() });

为了提供循环依赖(就是当A使用B的时候B已经初始化),需要使用OnActivated事件接口:

1
builder.Register(c => new A()).OnActivated(e => e.Instance.B = e.Context.Resolve<B>());

通过发射,使用PropertiesAutowired()修饰符注入属性。

1
builder.RegisterType<A>().PropertiesAutowired();

如果你预先知道属性的名字和值,你可以使用

1
builder.WithProperty("propertyName", propertyValue)。

相关文章:

  • 2021-06-01
  • 2021-05-24
  • 2021-08-28
  • 2021-09-02
  • 2022-02-25
  • 2021-10-06
  • 2022-12-23
  • 2021-10-13
猜你喜欢
  • 2021-06-25
  • 2022-01-10
  • 2022-01-06
  • 2021-06-14
  • 2021-07-13
  • 2021-09-24
  • 2021-08-24
相关资源
相似解决方案