简体   繁体   English

将Delegate []传递给方法

[英]Passing a Delegate[] to a method

I am trying to desconstruct a method written by another coder to see how it works but it's getting a bit confusing 我正在尝试解构由另一个编码器编写的方法,以了解其工作原理,但有点令人困惑

We have a Delegate that takes an Action as parameter. 我们有一个将Action作为参数的委托。

private delegate void FunctionDelegate(Action next);

A function is then called in the constructor that takes an array of these delegates as parameter and executes each value in the array 然后在构造函数中调用一个函数,该函数将这些委托的数组作为参数并执行数组中的每个值

LoadInSeries(LoadRoleAreaHours, LoadTableData);

The Function looks like this 函数看起来像这样

private void LoadInSeries(params FunctionDelegate[] _delegates)
        {

          var Delegates = new List<FunctionDelegate>(_delegates);

           Func<Action, Action> creator = null;
            creator = delegate(Action inner)
            {
                if (Delegates.Count > 0)
                {
                    FunctionDelegate L = Delegates.First();
                    Delegates.RemoveAt(0);
                    Action next = delegate { L(inner); };
                    return creator(next);
                }
                else return inner;
            };
            Action outer = creator(null);

             if (outer != null)
                outer();
        }

The intent was too chain a series of functions with each function calling the next but wouldnt it just be easy to use a multicast delegate and add each function to the invocation list. 目的是将一系列函数链接在一起,每个函数都调用下一个函数,但是使用多播委托并将每个函数添加到调用列表中不会很容易。

Does this code do something different? 这段代码有什么不同之处吗?

Without a good Minimal, Complete, and Verifiable code example , it's impossible to understand the code fully. 没有良好的最小,完整和可验证的代码示例 ,就不可能完全理解代码。 Of most significant concern is that your code example includes a mysterious L() method, the behavior of which we have no idea. 最重要的问题是您的代码示例包含一个神秘的L()方法,我们对此一无所知。 There is also the problem with the FunctionDelegate = Delegates.First(); FunctionDelegate = Delegates.First();也存在问题FunctionDelegate = Delegates.First(); statement. 声明。 At best, the FunctionDelegate identifier refers to a class field or property; 充其量, FunctionDelegate标识符引用一个类字段或属性。 at worst, that statement won't even compile. 最糟糕的是,该语句甚至无法编译。 Either way, there's no evidence that the delegate object being removed from the input list is ever actually invoked. 无论哪种方式,都没有证据表明实际上已经从输入列表中删除了委托对象。

So to even answer the question, some basic assumptions have to be made, which may or may not be correct. 因此,即使要回答这个问题,也必须做出一些基本假设,这些假设可能正确也可能不正确。

That said, in the best-case scenario — which is that the code has hidden some awful, convoluted mechanism in the L() method that ultimately winds up able to invoke the delegate for the current call to the creator delegate — the code you're looking at is not simply invoking delegates in sequence, as would be the case with a MulticastDelegate . 就是说,在最佳情况下,即代码在L()方法中隐藏了一些令人费解的复杂机制,最终最终可以为当前对creator委托的调用调用委托。重新查看不仅仅是按顺序调用委托,就像MulticastDelegate Rather, the code is constructing a chain of calls to the L() method, passing each delegate instance to the method in sequence. 相反,代码正在构造对L()方法的调用链,将每个委托实例依次传递给该方法。

Since you didn't show us the L() method, there's no way to say what the code actually does. 因为你没有向我们展示L()方法,也没有办法说什么代码实际上做。 I would agree that if all that L() does is invoke the delegate you pass to it, then this code looks like a very complicated way to just invoke an array of delegates. 我同意,如果L()所做的只是调用传递给它的委托,那么此代码看起来就像是一种非常复杂的方法,它仅调用一个委托数组。 But, giving the benefit of the doubt to the person who wrote the code, this simply means that L() probably does something other than simply invoke the delegate. 但是,将疑问的好处带给编写代码的人,这仅意味着L()可能会执行其他操作,而不仅仅是调用委托。

Of course, it's possible the author of the code doesn't deserve the benefit of the doubt. 当然,代码的作者可能不值得怀疑。 In that case, not only would it be simpler to just use a multicast delegate, the simplest implementation would just iterate over the array, invoking each delegate in the desired sequence. 在那种情况下,不仅使用多播委托会更简单,而且最简单的实现只是遍历数组,以所需的顺序调用每个委托。 But I say that without really knowing what the code does. 但是我说的并不是真的知道代码是做什么的。 I'm just assuming it's intended to do something useful with the delegates that are passed to it. 我只是假设它打算对传递给它的委托做一些有用的事情。 There's no evidence in the code you posted to support even that generous assumption. 您发布的代码中没有任何证据支持这种慷慨的假设。

Give us the full picture, and a more definitive answer can be provided. 请给我们完整的图片,然后可以提供更明确的答案。 Without knowing what L() is, or what side effects might exist in the passed-in delegates' target methods, it's impossible to say for sure whether the code you're looking at really needs to be written that way or not. 如果不知道L()是什么,或者传入的委托的目标方法中可能存在哪些副作用,就无法确定要查看的代码是否确实需要以这种方式编写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM