代码之家  ›  专栏  ›  技术社区  ›  KevinT

注册需要构造函数参数的组件,该构造函数参数是没有接口的具体类型

  •  0
  • KevinT  · 技术社区  · 16 年前

    public class DirectorySync : IDirectorySync
    {
      public DirectorySync(DirectoryInfo sourceDir, DirectoryInfo targetDir)
      {
        _sourceDirectory = sourceDir;
        _targetDirectory = targetDir;
      }
    }
    

    如何指定DirectoryInfo实例?它们应该是:

    var sourceDirectory = new DirectoryInfo("some known file path");
    var installationDirectory = new DirectoryInfo("some other known file path");
    

    _container.Register(Component
      .For<IDirectorySync>()
      .ImplementedBy<DirectorySync>()
      .Parameters(Parameter.ForKey("sourceDir").Eq(???))
      .Parameters(Parameter.ForKey("targetDir").Eq(???))
      .LifeStyle.Is(LifestyleType.Transient));
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   KevinT    16 年前

    知道了!

    _container.Register(Component
                    .For<IDirectorySync>()
                    .ImplementedBy<DirectorySync>()
                    .LifeStyle.Is(LifestyleType.Transient)
                    .DependsOn(new
                               {
                                   sourceDir = new DirectoryInfo("some known file path"),
                                   targetDir = new DirectoryInfo("some other known file path")
                               })
                    );