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:
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.
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).
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);
It is possible to define interfaces so that arbitrary combinations of them may be required and used in type-safe fashion. The key is to define an interface ISelf(Of Out T), whose one member, Self, is a property by which the object returns itself as a T, and then for each interface IFoo, declare a corresponding generic of the form IFoo(Of Out T), which inherits from both IFoo and ISelf(Of T). A class Wowzo which implements ISelf(Of Wowzo), IFoo(Of Wowzo), IBar(Of Wowzo), and IBoz(Of Wowzo), will implemenet IFoo(Of IBar), IBar(Of IFoo), IFoo(Of IBoz(Of IBar)), IFoo(Of IBar(Of IFoo(Of IBar(Of IBoz(Of IBar))))), etc. and may if necessary be typecast to any such type. If Thing is an IFoo(Of IBar(Of IBoz)), one can use it directly as an IFoo, or use Thing.Self as an IBar, or use Thing.Self.Self as an IBoz.
Example of how to compose three interfaces together this way with C#:
public interface ISelf<out T>
{
T Self { get; }
}
interface IA { }
interface IA<T> : IA, ISelf<T> { }
interface IB { }
interface IB<T> : IB, ISelf<T> { }
interface IC { }
interface IC<T> : IC, ISelf<T> { }
class ConcreteThing
: IA<ConcreteThing>
, IB<ConcreteThing>
, IC<ConcreteThing>
{
public ConcreteThing Self => this;
}
public class ReferencingObject
{
IA<IB<IC>> Thing { get; }
void Method()
{
IA a = Thing;
IB b = Thing.Self;
IC c = Thing.Self.Self;
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With