I have two generic Interface
public interface IFunction<TParam, TResult>
{
TResult Execute(TParam param);
}
public interface IFunctionService<TFunction, TParam, TResult>
where TFunction : IFunction<TParam, TResult>
{
IEnumerable<TResult> Execute(TParam param);
void RegisterFunction(TFunction function);
}
If I want to use IFunctionService I have to write code like this:
IFunctionService<IFunction<string, bool>, string, bool> _Service;
As you can see I have to write TParam and TResult twice. And thats not very nice. Has anybody an idea how to make this nicer? I would like some code like this:
public interface IFunctionService<TFunction>
where TFunction : IFunction<TParam, TResult>
{
Type TParam = typeof(TFunction).GetGenericArguments()[0];
Type TResult = typeof(TFunction).GetGenericArguments()[1];
IEnumerable<TResult> Execute(TParam param);
void RegisterFunction(TFunction function);
}
With typeof(TFunction).GetGenericArguments()[0] I would get the correct Type, but thats not possible in an Interface.
Any idea? Do you need to know anything else?
Thanks in advance! Lukas
Is the
TFunctiontype really necessary in yourIFunctionServiceinterface declaration?Could you refactor it to something like this?