C
C#8mo 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> {}
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(...);
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"
{ ... }
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: https://discord.com/channels/143867839282020352/1241306845940027472)
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server