使用castle windsor的构造函数的两个字符串值

本文关键字:两个 字符串 castle windsor 构造函数 使用 | 更新日期: 2023-09-27 18:22:20

我目前正在尝试这个:

var dependencies = new Dependency[2];
dependencies[0] = Dependency.OnValue("x", "xV");
dependencies[1] = Dependency.OnValue("y", "yV");
container.Register(
    Component.For<IBla>().ImplementedBy<BlaConcrete>().DependsOn(dependencies));

不幸的是,它似乎不起作用。例外情况是:

Could not resolve non-optional dependency

简化的混凝土如下所示:

public class Bla : IBla
{
    private readonly string _x;
    private readonly string _y;
    public Bla(string x, string y)
    {
        _x = x;
        _y = y;
    }
]

使用castle windsor的构造函数的两个字符串值

您的代码中一定有其他东西导致了问题,因为在一个空项目中,此代码按预期工作:

static void Main(string[] args)
{
    WindsorContainer container = new WindsorContainer();
    var dependencies = new Dependency[2];
    dependencies[0] = Dependency.OnValue("x", "xV");
    dependencies[1] = Dependency.OnValue("y", "yV");
    container.Register(Component.For<IBla>().ImplementedBy<Bla>().DependsOn(dependencies));
    var bla = container.Resolve<IBla>();
}
public interface IBla 
{
}
public class Bla : IBla
{
    readonly string _x;
    readonly string _y;
    public Bla(string x, string y)
    {
        _x = x;
        _y = y;
    }
}

将断点放在Resolve调用上表明bla具有"xV""yV"作为其内部成员的值。

在您检查了抛出的异常以了解更多详细信息(我保证会有更多)之后,我建议您将应用程序的某些部分分段删除,看看哪一部分会对此产生影响。或者,从一个像这样的空项目开始,添加可能影响这一点的应用程序部分。