I try to use Ninject to inject a XmlReader. The problem is that it is created by a factory method insted of a constructor. And I can't add a [Inject]
to code in the .NET Framework. Now I use following binding to create the XmlReader:
Bind<IXmlReader>()
.ToMethod(
x =>
XmlReader.Create(
(string) GetParameter(x, "inputUri"),
(XmlReaderSettings) GetParameter(x, "settings")))
.Named("definition");
private object GetParameter(IContext context, string name)
{
var parameters = (List<IParameter>) context.Parameters;
return (from p in parameters
where p.Name == name
select p.GetValue(context))
.FirstOrDefault();
}
And I use it as following:
var reader = _kernel.Get<IXmlReader>("definition",
new Parameter("inputUri", FilePath, false),
new Parameter("settings", settings, false)))
But this code is horrible. Can I rewrite it in any prettier smarter way?