views:

126

answers:

5

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.

A: 

you will have to define third type (possibly interface) that inherits from both interfaces. if you have such constraints then definitely you should have one. otherwise it is unusable. if this (obj is IB && obj is IB) then obj is exactly that type.

Andrey
+3  A: 

I agree with the other responders that you probably have a design issue if you need to do this, but you could accomplish it with a proxy object that implements both interfaces and delegates the calls to the two casted interface instances of the unknown Object. Now, when you call this method, you can construct the proxy for any type that supports both interfaces.

Dan Bryant
The problem is that the proxy object is not reference-equals to the original.
Bruno Martinez
+2  A: 

It seems that you're misunderstanding how generics work: when calling a method which has a generic parameter T, T must be statically known at compile time. Although the compiler can sometimes infer it (and you therefore don't always need to explicitly write it down), some T must be supplied when calling the method. In your case, all you know is that obj is an IA and an IB, but this doesn't give you enough information to call foo<T>, since you have no idea what T ought to be. You'll either have to use reflection, cast to a specific type which implements both IA and IB, or make a more dramatic design change.

kvb
+2  A: 

Your bar method should be generic too, with the same constraints as foo.

But if you really want to solve this problem, you can create a wrapper for an object that implements both interfaces that delegates all calls to the wrapped instances:

class ABWrapper : IA, IB {
  IA _a;
  IB _b;
  public Wrapper(IA a) {
    if (!(a is IB)) throw new ArgumentException();
    _a = a;
    _b = (IB)a;
  }
  public Wrapper(IB b) {
    if (!(b is IA)) throw new ArgumentException();
    _a = (IA)b;
    _b = b;
  }
  // explicit implementation for IA and IB delegating to _a and _b
}

And use it like this:

static void bar(object obj) {
  if (obj is IA && obj is IB) {
    foo(new ABWrapper((IA)obj)); 
  }
}
Jordão
+1  A: 

Does the C# 4.0 dynamic keyword get you out of jail (mostly) free? After all - you are already doing the type checking.

interface IC : IA, IB { }

void bar(object obj)
{
  if (obj is IA && obj is IB)
  {
    IC x = (dynamic)obj;
    foo(x);
  }
}

Does that break if foo tries to cast the parameter to T? I don't know.

David B
Thanks! Casting to IC doesn't actually work, but foo((dynamic)obj) does.
Bruno Martinez
Ah neat, I guess the T in that call is resolved to 'dynamic'
David B
I'm not sure. foo((dynamic)obj) throws if obj doesn't implement IB, even if foo calls no interface method from IB. There's no late binding.
Bruno Martinez
T must be resolved by the compiler. Maybe it's 'dynamic where : IA, IB'... then the runtime checks the type of the instance according to that rule on it's way into the method... that sounds really safe.
David B