例如,有这么一个类:
public class TextFileLog:ILog
{
private string target;
private ILogFormatter format;
public TextFileLog(string target, ILogFormatter format)
{
this.target = target;
this.format = format;
}
public string Write(string MsgStr)
{
return “output:” + format.Format(MsgStr + “-” + target);
}
}
现在需要通过xml注入:
<component id=”txtLog” type=”TestWindsor1.Entity.TextFileLog,TestWindsor1″ service=”TestWindsor1.Interface.ILog,TestWindsor1″>
<parameters>
<target>c:\log2018.txt</target>
<format>
<parameters>
<target>DayOne</target>
</parameters>
</format>
</parameters>
</component>
Castle.Core.Resource.ConfigResource source = new Castle.Core.Resource.ConfigResource();
XmlInterpreter interpreter = new XmlInterpreter(source);
WindsorContainer windsor = new WindsorContainer(interpreter);
IKernel kernel = windsor.Kernel;
ILog log = kernel.Resolve<ILog>(“txtLog”);
Response.Write(log.Write(“hello world”));
运行时报错:Could not convert parameter “format” to type “ILogFormatter”.
public class TextFileLog:ILog
{
private string target;
private ILogFormatter format;
public TextFileLog(string target, ILogFormatter format)
{
this.target = target;
this.format = format;
}
public string Write(string MsgStr)
{
return “output:” + format.Format(MsgStr + “-” + target);
}
}
现在需要通过xml注入:
<component id=”txtLog” type=”TestWindsor1.Entity.TextFileLog,TestWindsor1″ service=”TestWindsor1.Interface.ILog,TestWindsor1″>
<parameters>
<target>c:\log2018.txt</target>
<format>
<parameters>
<target>DayOne</target>
</parameters>
</format>
</parameters>
</component>
Castle.Core.Resource.ConfigResource source = new Castle.Core.Resource.ConfigResource();
XmlInterpreter interpreter = new XmlInterpreter(source);
WindsorContainer windsor = new WindsorContainer(interpreter);
IKernel kernel = windsor.Kernel;
ILog log = kernel.Resolve<ILog>(“txtLog”);
Response.Write(log.Write(“hello world”));
运行时报错:Could not convert parameter “format” to type “ILogFormatter”.
解决方案
40
一个接口,你让本人拿什么创建?