C#C
C#2y ago
Verdur

Check whether list of generics contains a type, that implements interface

Hello, I need to perform sophisticated generic type constraint.
I have following class to store multiple generics in my class, they are added to class after each method call:
interface ICons<TLeft, TRight> {}

And after calling few methods I get following type:
class Builder<T> { ... }

Builder<ICons<IWithBar, ICons<IWithFoo, IUninitialized>> builder = new Builder().Foo(...).Bar(...);

I have a function that accepts Builder<IConst<TLeft, TRight>> and I need generic constraint that will accept only those Builder's, which have both IWithFoo and IWithBar somewhere inside their IConst<TLeft, TRight>.
Something like this:
void Build<TLeft, TRight>(Builder<ICons<TLeft, TRight>> builder)
where "TLeft or TRight contain somewhere inside them IWithFoo and IWithBar"
{ ... }


I tried another approach where I specified that Build<T> accepts T where T : IWithBar, IWithFoo, but that would require ICons<TLeft, TRight> to implement TLeft and TRight, which I was answered is not possible (here: helpInterface implementing generic parameters)
Discord
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
Was this page helpful?