Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 一个类的属性作为参数传递给另一个类的构造函数。如何使用Autofac解决此问题?_C#_Dependency Injection_Autofac - Fatal编程技术网

C# 一个类的属性作为参数传递给另一个类的构造函数。如何使用Autofac解决此问题?

C# 一个类的属性作为参数传递给另一个类的构造函数。如何使用Autofac解决此问题?,c#,dependency-injection,autofac,C#,Dependency Injection,Autofac,我是Autofac的新手,这是我的问题所在: 我有两个班,都是单身 其中一个有一些公共财产,例如 public class ClassWithProperty { public string SomeProperty { get; set; } } 第二个类有一个构造函数,它应该以第一个类的属性作为参数: public class ClassWithConstructor { private string _someParameter; public ClassWit

我是Autofac的新手,这是我的问题所在:

我有两个班,都是单身

其中一个有一些公共财产,例如

public class ClassWithProperty
{
  public string SomeProperty { get; set; }
}
第二个类有一个构造函数,它应该以第一个类的属性作为参数:

public class ClassWithConstructor
  {
    private string _someParameter;

    public ClassWithConstructor(string someParameter)
    {
      _someParameter = someParameter;
    }
  }
没有Autofac,我可以这样做:

var classWithProperty = new ClassWithProperty();
var classWithConstructor = new ClassWithConstructor(classWithProperty.SomeProperty);
我无法用Autofac解决这个问题,也无法在这里或谷歌找到任何解决方案。 我所做的:

var builder = new ContainerBuilder();

builder.RegisterType<ClassWithProperty>().InstancePerLifetimeScope();
builder.RegisterType<ClassWithConstructor>()
    .WithParameter() // what should be done here to pass exactly ClassWithProperty.SomeProperty here?
    .InstancePerLifetimeScope();
var container = builder.Build();
var builder=newcontainerbuilder();
builder.RegisterType();
builder.RegisterType()
.WithParameter()//在这里应该做什么才能准确地传递ClassWithProperty.SomeProperty?
.InstancePerLifetimeScope();
var container=builder.Build();

当然,这是一个简化的场景,只是为了说明我的问题。在实际场景中,我将树列表从一个表单传递到另一个视图类,并精确地处理该树列表。

您可以注册lambda,用构造函数手动构造

var builder = new ContainerBuilder();

builder.RegisterType<ClassWithProperty>()
       .InstancePerLifetimeScope();
builder.Register(c => new ClassWithConstructor(c.Resolve<ClassWithProperty>().SomeProperty))
       .InstancePerLifetimeScope(); 
var builder=newcontainerbuilder();
builder.RegisterType()
.InstancePerLifetimeScope();
Register(c=>newclasswithconstructor(c.Resolve().SomeProperty))
.InstancePerLifetimeScope();

var builder=newcontainerbuilder();
builder.RegisterType()
.InstancePerLifetimeScope();
builder.Register()
.WithParameter(新解析参数(
(pi,ctx)=>pi.ParameterType==typeof(string)和&pi.Name==“someParameter”,
(pi,ctx)=>“节名”))
.InstancePerLifetimeScope();
从阅读开始。请注意如何解析
——执行此操作,并读取其中的属性。
var builder = new ContainerBuilder();

builder.RegisterType<ClassWithProperty>()
       .InstancePerLifetimeScope();
builder.Register<ClassWithConstructor>()
       .WithParameter(new ResolvedParameter(
              (pi, ctx) => pi.ParameterType == typeof(string) && pi.Name == "someParameter",
              (pi, ctx) => "sectionName"))
       .InstancePerLifetimeScope();