The Orange Tea Frog
❔ How can I avoid supplying redundant generic type arguments in this case.
This is what I currently have. To give some context, I have a number of custom data structures (implementing IDataStructure) and I would like Effect to be a generic class that can operate on any of them. However, I need access to the Item and Locator types of the data structure within the Effect class.
The above code works however the I and L type arguments are essentially redundant since they can be inferred from D. I would like something like
class Effect<D<I,L>> where D : IDataStructure<I,L> { }
(which doesn't compile) so that I can do `new Effect<MyStringArray>()' (only specifying one generic type argument but having access to the Item and Locator types within the Effect class).6 replies
✅ How can I get multiple responses from raising an event?
Typically, when you raise an event you don't expect a response. However, you can define you own delegate that has a non-void return type which will mean that a subscriber can return a value. Alternatively, you can pass a mutable object in the
EventArgs
which the subscriber can change and then that change can be seen by the publisher.
I want to extend this to the case where there are multiple subscribers. The first option doesn't work since you only get the return value of the last subscriber to execute. You can put a List
in the EventArgs
and then have the subscribers add their responses to it, however, I don't see any way of doing this that would ensure that each of the subscribers cannot see the responses of other subscribers while allowing the publisher to see all of the responses. The best I have come up with is to have the EventArgs
be readable once (e.g. raising an exception or setting a flag if it is tried to be read more than once) which would allow the publisher to see if any of the subscribers have been looking at the other responses. Is there a better way of doing this?22 replies