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

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.

Thanks! Casting to IC doesn't actually work, but foo((dynamic)obj) does. – Bruno Martinez May 25 '10 at 15:09 Ah neat, I guess the T in that call is resolved to 'dynamic' – David B May 25 '10 at 16:20 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 May 25 '10 at 16:31 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 May 25 '10 at 16:45.

I don't believe what you want is possible if there is no type or interface which is applicable to all objects that you might want to accept. If you have control over the types of things that you'll be handling, you should define an interface which "inherits" all your constraints, and then have the function accept a parameter of that type. If you also want to include a base type in your constraint, define an interface: Interface ISelf(Of Out T) Function Self As T End Interface and then have the composite constraint interface inherit ISelf(Of DesiredBaseType).

For example, if you are going to have a routine accept objects which derive from type Customer and implement both IDisposable and IEnumerable(Of String) silly arbitrary example, define: Interface IDisposableEnumerableOfStringAndSelf(Of T) Inherits IDisposable, IEnumerable(Of String), ISelf(Of T) End Interface and then have those routines accept an IDIsposableEnumerableOfStringAndSelf(Of Customer). Note that this will only work if the class being passed in explicitly implements either IDIsposableEnumerableOfStringAndSelf(Of Customer) or else IDIsposableEnumerableOfStringAndSelf(Of T) for some T that's a subtype of Customer. Interfaces are not duck-typed (a fact which is important, since it's possible and sometimes useful to have an interface with no members, e.g.To indicate that a class promises to be immutable).

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)); } }.

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, 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.

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.

The problem is that the proxy object is not reference-equals to the original. – Bruno Martinez May 24 '10 at 23:49.

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.

1 Even if InterfaceC does nothing more than inherit InterfaceA and InterfaceB, an object cannot be cast to InterfaceC unless it explicitly implements it. Even if the object implements both InterfaceA and InterfaceB, a cast to InterfaceC will fail. – supercat Dec 1 '10 at 0:53.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions