Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# "OR" event delegates returning bool

I have created a console for my game in XNA and I have a delegate for when a command has been entered. At the moment the delegate is returning a bool value. I have declared an event inside the Console class (which returns false) and then subscribed to this event from other classes. The idea is, if none of the classes that subscribe to this event return true then it is assumed that the user has entered an invalid command. However if at least one of the subscribed classes returns true then the command is assumed to be valid.

At the moment, only one class is taken into account for returning true or false, is there a way I can look at the return values of all the subscribing classes then OR their result?

Thanks,

like image 487
Dave Avatar asked Sep 18 '10 06:09

Dave


2 Answers

From within the class that declares the event, you can retrieve the invocation-list of the event (assuming a field-like event). Invoking each of them individually will allow you to inspect the return value of each subscriber to the event.

For example:

public event Func<bool> MyEvent = delegate { return false; };

...     

private bool EmitMyEventAndReturnIfAnySubscriberReturnsTrue()
{
    return MyEvent.GetInvocationList()
                  .Cast<Func<bool>>()
                  .Select(method => method()) 
                  .ToList() //Warning: Has side-effects
                  .Any(ret => ret);
}

In this example, every subscriber is informed of the event - no short-circuit occurs if any of them responds affirmatively. This behaviour can be easily changed if desired by removing the call to ToList().

To be honest though, I don't really like events that return values; their semantics are not obvious to subscribers. I would change the design if at all possible.

EDIT: Corrected error in forcing full execution of the sequence based on Timwi's comment.

like image 180
Ani Avatar answered Oct 21 '22 01:10

Ani


I figured it out just as I posed this question :P

bool handled = false;
foreach (Delegate d in CommandProcessed.GetInvocationList())
    handled |= (bool) d.DynamicInvoke (gameTime, command.ToString());

if (!handled) { } // Command Unrecognized

Where CommandProcessed is my event that classes subscribe to.
My delegate takes two arguments: gametime and command string.

like image 20
Dave Avatar answered Oct 21 '22 03:10

Dave