Casting an object to two interfaces at the same time, to call a generic method

Posted by Bruno Martinez on Stack Overflow See other posts from Stack Overflow or by Bruno Martinez
Published on 2010-05-24T17:30:53Z Indexed on 2010/05/25 0:21 UTC
Read the original article Hit count: 512

Filed under:
|
|
|
|

I want to call a generic method that constrains the input type T to implement two interfaces:

interface IA { }
interface IB { }
void foo<T>(T t) where T : IA, IB { }

How can I fix the last line of

void bar(object obj)
{
    if (obj is IA && obj is IB)
    {
        foo((IA && IB)obj);
    }
}

?

Reflection probably allows to do the call, but I would like to stay within the language.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET