tags:

views:

108

answers:

3

Similar to this Java question. I would like to specify that a variable implements multiple interfaces. For instance

private {IFirstInterface, ISecondInterface} _foo;

public void SetFoo({IFirstInterface, ISecondInterface} value)
{
    _foo = value;
}

Requirements:

  • I don't have the ability to add an interface to most type that would be passed in to Foo. So I can't create a third interface that inherits from IFirstInterface and ISecondInterface.
  • I would like to avoid making the containing class generic if possible because the type of Foo doesn't have much to do with the class and the user isn't likely to know it at compile time.
  • I need to use foo to access methods in both interfaces at a later time.
  • I would like to do this in a compiler safe way, i.e. no trying to cast to the interface just before trying to use it. If foo does not implement both interfaces quite a bit of functionality won't work properly.

Is this possible?

Edit: I've wanted this a few times for various reasons. In this instance it's because I am changing a set of properties from ObservableCollections to ReadOnlyObservableCollections. I have a helper class that creates a projection from a source ObservableCollection to another Collection. Since ReadOnlyObservableCollection does not inherit from ObservableCollection and I only need operations in IList and INotifyCollectionChanged I was hoping to store my source collection as a combination of these two interfaces rather than requiring an ObservableCollection.

+2  A: 

A variable in .NET has a type, and types can implement multiple interfaces. Something like this:

public interface Interface1 { }
public interface Interface2 { }
public class SupportsMuliple : Interface1, Interface2 { }

But from your question it seems like you want to have a variable implement multiple interfaces without having a type that implements those interfaces. This isn't really possible, but you could generate a type on the fly that implements the interfaces and hide the fact that there is a real type doing the magic. The mocking library moq does this using Castle DynamicProxy.

jkohlhepp
Thanks for response. The types that implement the interfaces already exist, I just need a way to store and use them in an implementation agnostic way.
Bryan Anderson
+7  A: 

If you want to constrain your method SetFoo to take only parameters that implement these two interfaces, then you are in luck:

public void SetFoo<T>(T value) where T : IFirstInterface, ISecondInterface
{
    _foo = value;
}

From this point on, any time you want to access your _foo member as one of either of these two interfaces, you'll just have to access it via casts: (IFirstInterface)_foo or (ISecondInterface)_foo, respectively.

You did say you'd like to avoid resorting to casts for compile-time safety; but I think, if you figure out a way to ensure that _foo gets initialized using SetFoo above, you can cast all you want with peace of mind.


I just realized the below describes something you specifically stated in the question you didn't want to do. So, I say, go with the above.

Another idea would be, since it looks like this _foo object is a class member (I'm basing this assumption on the _ in the variable name), you could define your class in this way:

class YourClassThatHasAFoo<T> where T : IFirstInterface, ISecondInterface {
    private T _foo;

    public void SetFoo(T value) {
        _foo = value;
    }
}

Whether this actually makes sense depends, obviously, on your specific scenario. You'll definitely have to think it through, as classes with constraints like this sometimes lead to issues you might not have expected down the road (such as that SetFoo now must take a parameter of type T, not just any class that implements your two interfaces).

Dan Tao
+3  A: 

No, there is no way to declaratively ensure that a particular instance implements more than one interface.

One option MIGHT be possible using generics, though this would really only work for a function rather than a property.

public void Foo<T>(T arg)
    where T : IFirstInterface
    where T : ISecondInterface
{
    ...
}

Using type inference, you should be able to call it like this:

ConcreteType bar = new ConcreteType(); // this implements both

Foo(bar);
Adam Robinson