如何在运行时操作委托的调用列表? [英] How to manipulate at runtime the invocation list of a Delegate?

查看:32
本文介绍了如何在运行时操作委托的调用列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从父委托的调用列表中删除单个委托.

I want to know how do I go about removing individual delegates from the invocation list of the parent delegate.

<DelegateName>.RemoveAll();

现在我可以删除所有的,这只是为了让这个想法动起来,看看它是否应该起作用,但是任何被删除的代表都需要在运行时再次添加!!!

Now I can just remove all and that will work just to get the idea in motion and see if works which it should but then any delegates removed will need adding again at RunTime!!!

所以:...

foreach(Delegate d in <DelegateName>.getInvocationList)
{ // do something with 'D'?? }

既然我可以访问D",我可以将它放入 .Remove() 中,但它需要两个参数???请问我可以得到一些帮助吗?

Now that I have access to 'D' I can drop it into a .Remove() but it requies two arguments??? Could I get some help with this please?

最后,一旦删除了委托,重新添加它的最佳方法是什么?所以我假设 Reflection 可能会在这方面发挥作用,但由你们负责.

Lastly, once the delegate has been removed, what is the best way to re-add it again? So i'm assuming Reflection might play a part in this but over to you guys.

更新:

本质上有一个全局委托,它有许多事件存在于不同的 winform 类中,这些类是它的实例.所有这些事件都有一个处理程序,可以在事件被触发时进行处理.顶部的委托一直控制着所有与其签名匹配的处理程序被触发.我想在顶部处理这个委托,我想删除所有处理程序,然后在运行时重新添加它们.所以顶部的委托是一个多播委托,它的调用列表是一个单独的委托列表,这些委托又指向我添加的四个处理程序之一.我希望这是有道理的,这就是我正在做的事情的总体思路.

Essentially there is a global delegate that has many events living in different winforms classes that are instances of it. All of these events have a handler that handles the event when it is fired. The delegate all the way at the top governs all handlers that match its signature are fired. I want to mess about with this delegate at the top, I want to remove all handlers and then re-add them at run time. So the delegate at the top is a multicast delegate and its invocation list is a list of individual delegates that in turn point to one of the four handlers that I have added. I hope that makes sense, thats a general idea of whats I am doing.

推荐答案

你不能 - 委托是不可变的.您可以在不执行某些操作的情况下创建一个与旧委托等效的新委托,但您不能就地修改现有委托.

You can't - delegates are immutable. You can create a new delegate which is equivalent to the old one without certain actions, but you can't modify the existing one in-place.

这篇关于如何在运行时操作委托的调用列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆