axa
axa

Reputation: 520

How to check if a method argument implements multiple interfaces

Is there any way to use constraints to check if a method argument is implementing more than one interface?

Take this simple example which checks if TResponse has implemented IBaseSearchResponse :

public static TResponse Search<TResponse, TRequest>(TRequest args)
    where TResponse : IBaseSearchResponse {}

But i want to also know if it implements IBaseSearchProps. Ive tried to add a constraint by doing things like:

public static TResponse Search<TResponse, TRequest>(TRequest args)
    where TResponse : IBaseSearchArgs where TResponse : IBaseSearchProps {}

But this reports a constraints clause already used for type TResponse And:

public static TResponse Search<TResponse, TRequest>(TRequest args)
    where TResponse : (IBaseSearchArgs && IBaseSearchProps) {}

this is just illegal syntax

I apologize in advance if my question is ill prepared, answered elsewhere or if the answer is defined in the c# spec..., i did at least take a look for it here

Upvotes: 0

Views: 100

Answers (2)

Ousmane D.
Ousmane D.

Reputation: 56423

you'll need to separate them by a comma (,):

e.g.

public static TResponse Search<TResponse, TRequest>(TRequest args)
where TResponse : IBaseSearchArgs , IBaseSearchProps {...}

Upvotes: 6

It's a comma-delimited list:

public void F<T>(T t) 
    where T : IEnumerable, IFooBar
{
}

Upvotes: 2

Related Questions