Reputation: 117
My intentions is to chain few methods in Func and execute them one by one to get response regarding Function executed successfully or not. Below is my code but unable to Invoke func after getting it from GetInvocationList because it expecting some Method Name. Kindly Advise to fix it...
Func<bool> funcList = null;
funcList += Init;
funcList += Process;
foreach(var func in funcList.GetInvocationList()) {
bool execuationStatus = func();
}
Upvotes: 0
Views: 150
Reputation: 4164
Change your method to :
Func<bool> funcList = null;
funcList += Init;
funcList += Process;
foreach (var func in funcList.GetInvocationList())
{
var fn = func as Func<bool>;
bool execuationStatus = fn();
}
GetInvokationList returns Delegate[] and you cannot invoke on it. There is difference between Delegate class vs delegate keyword. As per MSDN documentation :
The Delegate class is the base class for delegate types. However, only the system and compilers can derive explicitly from the Delegate class or from the MulticastDelegate class. It is also not permissible to derive a new type from a delegate type. The Delegate class is not considered a delegate type; it is a class used to derive delegate types.
Most languages implement a delegate keyword, and compilers for those languages are able to derive from the MulticastDelegate class; therefore, users should use the delegate keyword provided by the language.
Upvotes: 1