I have a "dispatch map" defined as such:
private Dictionary<string, Func<DynamicEntity, DynamicEntity, IEnumerable<DynamicEntity>, string>> _messageProcessing;
This allows me to dispatch to different methods easily depending on the name of the DynamicEntity instance.
To avoid being hated by everyone who maintains the code forever more, is there any way of naming the parameters in the Func to give some clue to which DynamicEntity is which?
Func<T, TResult> defines a function that accepts one parameter (of type T) and returns an object (of type TResult).
Func is a delegate that points to a method that accepts one or more arguments and returns a value. Action is a delegate that points to a method which in turn accepts one or more arguments but returns no value. In other words, you should use Action when your delegate points to a method that returns void.
The caller's arguments passed to the function's parameters do not have to have the same names.
An Action type delegate is the same as Func delegate except that the Action delegate doesn't return a value. In other words, an Action delegate can be used with a method that has a void return type. It can contain minimum 1 and maximum of 16 input parameters and does not contain any output parameter.
You can't do it with the built-in Func
types, but it's easy enough to create your own custom delegate type and use it in a similar way:
_messageProcessing.Add("input", (x, y, z) => "output"); _messageProcessing.Add("another", (x, y, z) => "example"); // ... delegate string DispatchFunc(DynamicEntity first, DynamicEntity second, IEnumerable<DynamicEntity> collection); Dictionary<string, DispatchFunc> _messageProcessing;
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