Looking for the best way to implement something like the Multicast delegate functionally for async Func functions.
Originally my custom builder used a Multicast delegate to register Start and Stop events like this:
public class {
private Action<ConfigObj> startEvents;
public AddStartEvent(Action<ConfigObj> newStart)
{
startEvents+=newStart;
}
public Start()
{
startEvents?.Invoke(config);
}
}
What is the best way to do this with async Func lambdas? I have implemented something that works.
private List<Func<ConfigObj,Task>> at;
...
public Task RunFuncs()
{
if (at.Count == 0) {
return Task.CompletedTask;
}
else
{
Task[] tasks = new Task[at.Count];
int ind = 0;
foreach(var t in at)
{
tasks[ind] = t.Invoke(configObj);
ind++;
}
return Task.WhenAll(tasks);
}
}
Is there a better way to do this?