October 4, 2006
11:06 AM

This little scenario popped into my head, and had me stumped for a while.

Given the interfaces IA and IB,

interface IA { }
interface IB { }

and the generic method AandB which accepts an object of a type which implements both IA and IB.

void AandB<T>(T anb) where T : IA, IB
{
}

How can we call AandB in the following situation?

void Situation(IA a)
{
    if (a is IB)
    {
        // AandB(a); ??
    }
}

Hint: You may be surprised how easy it is.

Update: Ouch, sorry all, I made a mistake, my solution didn't actually work! Seeking a new one. I'm back to thinking this is impossible (excluding using reflection).

© Douglas Stockwell 2007
Creative Commons License Unless otherwise specified all "source code" examples are available for use under the Creative Commons Attribution-Noncommercial 3.0 License. Please contact me if you would like more flexible licensing terms.
Messenger Presence