0

I have an interface:

public interface IValidator<TIn, TOut>
    where TIn: class
    where TOut: class
{
    IValidator<TIn, TOut> SetNext(IValidator<TIn, TOut> next);

    ValueTask<TOut> ValidateAsync(TIn state);
}

But I need to have an ability to pass in SetNext function IValidator<in TIn, TOut> next do you have some ideas on how it can be achieved? I've tried

public interface IValidator<**in** TIn, TOut>
    where TIn: class
    where TOut: class
{
    IValidator<TIn, TOut> SetNext(IValidator<TIn, TOut> next);

    ValueTask<TOut> ValidateAsync(TIn state);
}

But it's not doing what I need, it applies to the second method param but not for the interface itself

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
IR Codder
  • 15
  • 3
  • This is not type safe. See [my explanation here](https://stackoverflow.com/q/70641265/5133585). – Sweeper May 09 '22 at 13:41
  • To achieve what you want, you could split this interface into 2, one having ValidateAsync and another with SetNext. This way, at least ValidateAsync could still be covariant and contravariant, while SetNext cannot, unless you change the return to void or ValueTask for instance – jalepi May 09 '22 at 21:05

0 Answers0