Contravariant Help
Given the following code:
How might I take an instance of
MySubscriber
and create a ISubscriber<MessageC>[]
with 2 values. The first being the ISubscriber<MessageA>
implementation, and the second being the ISubscriber<MessageB>
implementation?5 Replies
The end goal being
with an output of
use System.Type::GetInterfaces to get all the interfaces, then check each one against your target interface type with Type::IsAssignableFrom (or ...To)
Thats how you could collect different ones together in a general sense, but since you have explicit impls I presume that wont work, thinking about it. You'd have to wrap in a lambda/utility adapter that does more casting
After asking around on the MonoGame server one thing ive found is that my question probably wasnt very clear
My question is about contravariance at its core. Given the code above, the following is valid:
As
MySubscriber
implements both ISubscriber<MessageA>
and ISubscriber<MessageB>
, and the ISubscriber
interfaces utilizes the in keyword i can cast MySubscriber
directly to ISubscriber<MessageC>
. The issue is i want to be able to invoke all ISubscriber
implementations that can be casted to ISubscriber<MessageC>
.
Doing ((ISubscriber<MessageC>)new MySubscriber()).Process()
is valid, but only gives me the first one.
Its an issue because ISubscriber<MessageC>
is ambiguous, there are two interfaces that can meet that criteria. Even with reflection i cant seem to find a way to invoke the ISubscriber<MessageB>
implementation
What happens if you use MessageB in CreateDele?
Sorry for the lack of response, The same result happens regardless of the type parameter used. I found a work around for my personal use case so marking this as closed.