© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
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> {}
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>>
Builder<IConst<TLeft, TRight>>
and I need generic constraint that will accept only those
Builder
Builder
's, which have both
IWithFoo
IWithFoo
and
IWithBar
IWithBar
somewhere inside their
IConst<TLeft, TRight>
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>
Build<T>
accepts
T where T : IWithBar, IWithFoo
T where T : IWithBar, IWithFoo
, but that would require
ICons<TLeft, TRight>
ICons<TLeft, TRight>
to implement
TLeft
TLeft
and
TRight
TRight
, which I was answered is not possible (here: helpInterface implementing generic parameters)
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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Check What Generic Type a Class Implements
C#CC# / help
4y ago
✅ How to check whether a given list is present in the List of list
C#CC# / help
4y ago
Create dynamic class that implements interface with static properties
C#CC# / help
3y ago
❔ Check if list contains object with a specific property/variable value
C#CC# / help
4y ago