Suppose I have a MulticastDelegate that implements a universal delegate and contains several calls:
Func<int> func = null; func += ( )=> return 8; func += () => return 16; func += () => return 32;
Now this code will return 32:
int x = func();
I would like to know if there is (or better I should ask why it does not exist!) A C # language function with which you can access the results of all delegate calls, that is, get a list ({8,16,32}) ?
Of course, this can be done using the .NET Framework routines. Something like this will work:
public static List<TOut> InvokeToList<TOut>(this Func<TOut> func) { var returnValue = new List<TOut>(); if (func != null) { var invocations = func.GetInvocationList(); returnValue.AddRange(invocations.Select(@delegate => ((Func<TOut>) @delegate)())); } return returnValue; }
But I cannot understand from the system that there should be a better way, at least without casting (really, why is MulticastDelegate not universal when delegates are)?
source share