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.