Func Chain - 预期的方法名称

Func Chain - Method Name Expected

我的意图是将 Func 中的几个方法链接起来,并一个一个地执行它们,以获得有关 Function 是否成功执行的响应。下面是我的代码,但在从 GetInvocationList 获取它后无法调用 func,因为它需要一些方法名称。请建议修复它...

            Func<bool> funcList = null;
            funcList += Init;
            funcList += Process;
            foreach(var func in funcList.GetInvocationList()) {
                bool execuationStatus = func();

            }

将您的方法更改为:

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[] 并且您无法调用它。 Delegate class 与 delegate 关键字之间存在差异。根据 MSDN 文档:

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.