0

使用 autofac json 配置注册模块时,您可以传入属性和/或参数。

{
  "modules": [
    {
      "type": "MyProject.Core.MyModule, MyProject.Core",
      "properties": {
        "ModuleProperties": {
          "prop1": "value1",
          "prop2": "value2"
        }
      }
  ]
}
public class MyModule : Autofac.Module
{
    public Dictionary<string, string> ModuleProps { get; set; }

    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<MyType>.As<IMyInterface>();
    }
}

有没有办法传递ModuleProps给我刚刚注册的类型?

4

1 回答 1

0

所以我想通了。我的 Concert 类型MyType有一个带Dictionary<string, string> config参数的构造函数,当我注册类型时,我可以像builder.RegisterType<MyType>.As<IMyInterface>.WithParameter("config", Config).

于 2021-07-23T19:02:02.430 回答