I came across Generics open and closed constructed types this question and started toying around with typeof
. I am now wondering why I can do this:
var t = typeof(IPipelineBehaviour<,>);
but not this
var s = typeof(IPipelineBehaviour<,List<>>);
Here is a minimal example for use with https://dotnetfiddle.net/
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var t = typeof(IPipelineBehaviour<,>);
Console.WriteLine(t);
var s = typeof(IPipelineBehaviour<,List<>>);
Console.WriteLine(s);
}
}
public interface IPipelineBehaviour<TRequest,TResult>
{
}
I am trying to register an unbound generic for an MediatR.IPipelineBehaviour
for which I can assert, that TResult is of type OneOf
(see https://github.com/mcintyre321/OneOf ).
I tried to assert it in the class definition through a where
clause such as this
public class MyBehaviour<TRequest,TResult> : IPipelineBehaviour<TRequest,TResult>
where TRequest : IRequest<TResult>
where TResult : OneOf<???,Problem>
{
// Implementation goes here
}
but in that case I am lacking the Type indicated as ???
and now I am trying to approach it as follows:
services.AddTransient(typeof(IPipelineBehaviour<,Oneof<,>>), typeof(ValidationBehaviour<,OneOf<,>>);
but it does not work, because typeof(IPipelineBehaviour<,Oneof<,>>)
cannot be evaluated.