Core Problem
At the core of my problem is learning the correct syntax for providing an unbound generic as a type parameter to another generic type that will be pass to the typeof
operator:
typeof(IEnumerable<IEnumerable<>>))
What is the proper syntax for this?
Ninject Use Case
I would like to achieve the following in Ninject.
Supposed I have the following interfaces and implementations:
public interface IParser<in TIn, out TOut>
{
TOut Parse(TIn input);
}
public class IntParser : IParser<string, int>
{
public int Parse(string input)
{
return int.Parse(input);
}
}
public class SpaceSeperatedParser<T> : IParser<string, IEnumerable<T>>
{
private readonly IParser<string, T> _itemParser;
public SpaceSeperatedParser(IParser<string, T> itemParser)
{
_itemParser = itemParser;
}
public IEnumerable<T> Parse(string input)
{
return input.Split(' ').Select(_itemParser.Parse);
}
}
I would like to bind these types into my Ninject kernel. However, I am having a hard time binding SpaceSeperatedParser
in a generic manner, so that I don't have to enumerate every possible T
type.
Syntactically, this seems like this should do that I am looking for. Unfortunately, it is not even syntactically correct!
IKernel kernel = new StandardKernel();
kernel.Bind(typeof (IParser<string, int>)).To(typeof (IntParser));
// does not work -- syntax error
kernel
.Bind(typeof(IParser<string, IEnumerable<>>))
.To(typeof(SpaceSeperatedParser<>));
// works, but is not generic
// kernel
// .Bind(typeof (IParser<string, IEnumerable<int>>))
// .To(typeof (SpaceSeperatedParser<int>));
// should return a SpaceSeperatedParser<int>, with IntParser as _itemParser
var parser = kernel.Get<IParser<string, IEnumerable<int>>>();
Console.WriteLine(string.Join(", ", parser
.Parse("23 42 9 32 10")
.Select(i => i + 5)));
Does Ninject even support this?