"Clearing" all chained methods in delegate c#

I have a situation where I need to chain methods to a delegate D using the anonymous method syntax ( D += delegate(...){} ). However I need to make sure that other chained methods are removed before adding a new one.

I know I can do something like D -= a, D += b, but since I'm using an anonymous method, I'm not sure whether the -= will work (since I don't have an explicit name.).

If my reasoning is correct is there a way I can remove all chained methods using the anonymous syntax ?

Thanks in advance.


I had to retract my original answer, because it was so wrong and comments (under both question and my answer) provided some additional info.

For both event and delegate, you can simply assign null (or a new listener) with = to get rid of all other listeners - but only in the same class as the event/delegate is defined.

I have found out that it's not possible by regular means to remove all listeners from an event that is not declared in the same class, but you can do it with Reflection by finding the event/delegate and assigning null to it with FieldInfo.SetValue .

Here's a link to an older SO question, which might have had similar constraints to yours. This particular answer worked for me: https://stackoverflow.com/a/8108103/1659828


Delegate types are immutable types . If D is an instance of a delegate type, it consists of a fixed-length invocation list with one or more items.

So you can't "clear" the list. You also cannot create a new instance with an empty invocation list because a null reference (not an instance) is used for the case where no methods are present.

Example ( MyMethod and JonnysMethod are method groups, not delegate variables):

Action D = null;   // no object, no invocation list

D += MyMethod;     // null and new Action(MyMethod) are combined; result is assigned to D
// D is an instance, invocation list length is 1
D += JonnysMethod; // two instances are combined; D is assigned to new instance; original instances are no longer referenced and will "soon" be garbage collected
// D is an instance, invocation list length is 2

D -= MyMethod;     // D is assigned to a new instance again
D -= JonnysMethod; // D is assigned to null; no new instance

Take care: If D is really an event, be careful. If you're outisde the class/struct where D is defined, += and -= are calls to the accessors of the event. So they are not immediate delegate combinations in this case. If you're inside the class/struct that defines D , and if D is a "field-like" event, D can be used in two senses. As a delegate variable (the "hidden" backing field of the event), and as an event. The meaning of += and -= can the be interpreted in two ways. Until C# 3, it was interpreted as delegate combinantion directly on the backing field. In C# 4 and C# 5, the semantic is changed and += and -= are calls to the event accessors in this situation.

链接地址: http://www.djcxy.com/p/51448.html

上一篇: 删除使用lambda表达式添加的eventHandler

下一篇: “清除”委托中的所有链接方法c#