C# 使用castle windsor的构造函数的两个字符串值
我目前正在尝试:C# 使用castle windsor的构造函数的两个字符串值,c#,castle-windsor,C#,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(dependen
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));
简化混凝土如下所示:
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;
}
}
static void Main(字符串[]args)
{
WindsorContainer容器=新WindsorContainer();
var dependencies=新的依赖项[2];
依赖项[0]=依赖项.OnValue(“x”,“xV”);
依赖项[1]=依赖项.OnValue(“y”,“yV”);
container.Register(Component.For().ImplementedBy().DependsOn(dependencies));
var bla=container.Resolve();
}
公共接口IBla
{
}
公共类Bla:IBla
{
只读字符串x;
只读字符串_y;
公共Bla(字符串x、字符串y)
{
_x=x;
_y=y;
}
}
在Resolve
调用上放置断点后,会显示bla
将的“xV”
和的“yV”
作为其内部成员的值
在您检查抛出的异常以了解更多细节(我保证会有更多细节)之后,我建议您将应用程序的某些部分分块删除,以查看哪些部分会对此产生影响。或者,从一个像这样的空项目开始,添加可能会影响这一点的应用程序。什么是“似乎不起作用”呢?你有例外吗?(如果是,在哪里?)您是否得到了预期之外的结果?添加整个异常;它告诉您它无法解析哪个依赖项。还要在问题中添加
BlaConcrete
的构造函数。为什么要关闭此选项。这是一个合理的问题。如果没有,请至少证明你的投票是正确的。人们正在投票关闭它,因为在你添加细节之前(你可以添加更多细节),这是一个“此代码不起作用”的问题。好的,谢谢你确认我的原始代码起作用。我是个白痴,集成测试中没有从web.config文件中获取设置。
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;
}
}