使用温莎城堡的构造函数的两个字符串值
two string values for constructor using castle windsor
我目前正在尝试这个:
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;
}
]
你的代码中一定有其他原因导致了这个问题,因为在一个空项目中,这段代码按预期工作:
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"
作为其内部成员的值。
在您检查抛出的异常以获取更多详细信息后(我保证会有更多),我建议您分段删除应用程序的某些部分以查看哪些有影响对此。或者,从一个像这样的空项目开始,然后添加可能影响它的应用程序部分。
我目前正在尝试这个:
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;
}
]
你的代码中一定有其他原因导致了这个问题,因为在一个空项目中,这段代码按预期工作:
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"
作为其内部成员的值。
在您检查抛出的异常以获取更多详细信息后(我保证会有更多),我建议您分段删除应用程序的某些部分以查看哪些有影响对此。或者,从一个像这样的空项目开始,然后添加可能影响它的应用程序部分。