简体   繁体   English

如何获取委托调用的结果列表?

[英]How to get results list of delegate's invocation?

Let say, I have a MulticastDelegate that implements generic delegate and contains several calls: 假设我有一个MulticastDelegate ,它实现了通用委托并包含多个调用:

Func<int> func = null;
func += ( )=> return 8;
func += () => return 16;
func += () => return 32;

Now this code will return 32: 现在这段代码将返回32:

int x = func(); // x=32

I would like to know if there exists (or better I should ask why it doesn't exist!) the C# language feature using which is possible to get the access to results of all delegate's invocations, that means to get the list ({8,16,32})? 我想知道是否存在(或者更好,我应该问为什么它不存在!)使用C#语言特性可以访问所有委托调用的结果,这意味着获取列表({8 ,16,32})?

Of course it's possible to do the same using .NET framework routines. 当然,使用.NET框架例程也可以这样做。 Something like this will do the 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 can't get out from the system that there should be the better way, at least without casting (really, why MulticastDelegate is not generic when delegates are)? 但我不能从系统中得出应该有更好的方法,至少没有强制转换(真的,为什么MulticastDelegate在委托时不是通用的)?

No, there isn't a better way - when you invoke a multicast delegate, the result is just the result of the final delegate. 不,没有更好的方法 - 当您调用多播委托时,结果只是最终委托的结果。 That's what it's like at the framework level. 这就是框架层面的情况。

Multicast delegates are mostly useful for event handlers. 多播代理主要用于事件处理程序。 It's relatively rare to use them for functions like this. 将它们用于这样的功能是相对罕见的。

Note that Delegate itself isn't generic either - only individual delegate types can be generic, because the arity of the type can change based on the type. 请注意, Delegate本身也不是通用的 - 只有单个委托类型可以是通用的,因为类型的arity可以根据类型进行更改。 (eg Action<T> and Action<T1, T2> are unrelated types really.) (例如, Action<T>Action<T1, T2>确实是不相关的类型。)

You can accomplish what you want if you don't use a Func<int> , but an Action which takes a method as parameter which processes the return values. 如果不使用Func<int> ,则可以完成所需的操作,但可以使用方法作为处理返回值的参数。 Here is a small example: 这是一个小例子:

    static Action<Action<int>> OnMyEvent=null;

    static void Main(string[] args)
    {
        OnMyEvent += processResult => processResult(8);
        OnMyEvent += processResult => processResult(16);
        OnMyEvent += processResult => processResult(32);

        var results = new List<int>();
        OnMyEvent(val => results.Add(val));

        foreach (var v in results)
            Console.WriteLine(v);

    }

There is no way to get a) exceptions b) return values from delegates, only by sniffing into result list. 没有办法得到a)例外b)从委托返回值,只能通过嗅探到结果列表。 Another way is just to have list of delegates and manage it manually. 另一种方法是拥有代表列表并手动管理它。

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

相关问题 从委托调用列表中获取结果 - Get result from Delegate Invocation list 反射 - 将代理添加到另一个代理的调用列表 - Reflection - Add a Delegate to another Delegate's invocation list 如何在运行时操作Delegate的调用列表? - How to manipulate at runtime the invocation list of a Delegate? 如何在不实例化委托的情况下将方法添加到委托的调用列表中? - How to add methods to the invocation list of a delegate without instantiating the delegate? 如何获得Action委托调用的名称/详细信息? - How can I get the name / details of an Action delegate invocation? 订阅事件是否将处理函数添加到事件或事件委托的调用列表中? - Does subscribing to an event add the handler function to the event or the event delegate's invocation list? 如何编写扩展方法,以使多播C#委托中的调用列表成员顺序运行? - How to write extension method which will make invocation list members in multicast C# delegate run sequentially? 如何使用委托获取对列表中项目的引用 - How to use a delegate to get a reference to an item in a list C#的空条件委托调用线程是否安全? - Is C#'s null-conditional delegate invocation thread safe? 如何在C#中获取显式事件的调用列表 - How to get invocation list of an explicit event in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM