Let's say I have a type:
public class Transformer<T, TResult>
where T : IMessage
where TResult : IMessage
{
private Func<T, IEnumerable<TResult>> _transformer;
public Transformer(Func<T, TResult> transformer)
{
_transformer = null // ?
}
public Transformer(Func<T, IEnumerable<TResult>> transformer)
{
_transformer = transformer;
}
}
So in essence, I'd like to convert Func<T, TResult>
into Func<T, IEnumerable<TResult>>
in the first constructor.
I've tried to create a private inner class that takes Func<T, TResult>
and defined a method that returns IEnumerable like this:
private class TransformerWrapper
{
private readonly Func<T, TResult> _func;
public TransformerWrapper(Func<T, TResult> func)
{
_func = func;
}
public IEnumerable<TResult> Transform<T>(T message) where T : IMessage
{
yield return _func(message);
}
}
But it's not really working out. I get an error saying delegate has an invalid argument - cannot convert from T to T.
First of all, what is happening with the compiler error and second, is there another way?
Update
Minutes after I posted, I found a solution:
public Transformer(Func<T, TResult> transformer)
{
_transformer = new TransformerWrapper<T, TResult>(transformer).Transform;
}
And,
private class TransformerWrapper<T, TResult>
{
private readonly Func<T, TResult> _func;
public TransformerWrapper(Func<T, TResult> func)
{
_func = func;
}
public IEnumerable<TResult> Transform(T message)
{
yield return _func(message);
}
}
I still can't get my head around why the first solution did not work. I need to think about that one...