在解析Windsor依赖项时,我有以下例外:
无法解析'Namespace.MyClass1'(Namespace.MyClass1)的非可选依赖项。 参数'Namespace.MyClass2'类型'Namespace.MyClass2'
public class MyClass1
{
public MyClass1(MyClass2 myClass2, MyClass3 myClass3)
{
this.MyClass2 = myClass2,
this.MyClass3 = myClass3,
}
protected MyClass2 MyClass2 { get; private set; }
public MyClass3 MyClass3 { get; set; }
}
解析MyClass2和MyClass3没有问题。
**编辑 - 测试用例**
我正在使用Windsor 2.5.1(或2.5.2 - 今天通过NuGet下载)
所有代码:
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.Core.Resource;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
WindsorContainer container = new WindsorContainer(new XmlInterpreter(new ConfigResource()));
container.Register(
Component.For<MyClass2>(),
Component.For<MyClass1>()
.DependsOn(
Property.ForKey("myClass2").Is<MyClass2>(),
Property.ForKey("myClass3").Is<MyClass3>()
)
);
container.Resolve<MyClass1>();
}
}
public class MyClass3 { public MyClass3() {} }
public class MyClass2 { public MyClass2() {} }
public class MyClass1
{
public MyClass1(MyClass2 myClass2, MyClass3 myClass3)
{
this.MyClass2 = myClass2;
this.MyClass3 = myClass3;
}
protected MyClass2 MyClass2 { get; private set; }
public MyClass3 MyClass3 { get; set; }
}
}
App.config中:
<castle>
<properties>
</properties>
<include uri="file://windsor.config" />
</castle>
windsor.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<components>
<component id="hoho" type="ConsoleApplication9.MyClass3"/>
</components>
</configuration>