Removing delegates from chain
class Program
{
internal delegate int CallBack(int i);
static void Main(string[] args)
{
CallBack callbackMethodsChain = null;
CallBack cbM1 = new CallBack(FirstMethod);
CallBack cbM2 = new CallBack(SecondMethod);
callbackMethodsChain += cbM1;
callbackMethodsChain += cbM2;开发者_如何学Go
Delegate.Remove(callbackMethodsChain, cbM1);
/*L_0039: call class [mscorlib]System.Delegate [mscorlib]System.Delegate::Remove(class [mscorlib]System.Delegate, class [mscorlib]System.Delegate)
L_003e: pop
L_003f: ldloc.0 */
Trace.WriteLine(callbackMethodsChain.GetInvocationList().Length);
//Output: 2 **WTF!!!**
callbackMethodsChain -= cbM1;
/*
L_0054: call class [mscorlib]System.Delegate [mscorlib]System.Delegate::Remove(class [mscorlib]System.Delegate, class [mscorlib]System.Delegate)
L_0059: castclass Generics.Program/CallBack
L_005e: stloc.0
L_005f: ldloc.0
*/
Trace.WriteLine(callbackMethodsChain.GetInvocationList().Length);
//Output: 1
}
private static int FirstMethod(int test)
{
Trace.WriteLine("FirstMethod");
return test;
}
private static int SecondMethod(int test)
{
Trace.WriteLine("SecondMethod");
return test;
}
}
So, we always need to cast (CallBack)Delegate.Remove(callbackMethodsChain, cbM1); to remove delegate from chain. It's not obviously.
A delegate is immutable, which means you cannot change it. Any methods that seems to change it, like "adding" to it or "subtracting" from it, actually returns a new delegate with the changes.
So this would not work:
a.Remove(b);
But this would:
a = a.Remove(b);
in terms of calling the Remove method that is.
Note that the following syntax does the right thing:
a -= b;
This is why, after calling Remove, that you still observe the code calling the delegate you seemingly removed, you're still calling the original delegate chain with that delegate present.
Some Other points are
Duplicates are allowed in your delegate i.e your delegate can have something like [cbM1,cbM2,cbM2,cbM3]
If u have method group [cbM1,cbM2, cbM3,cbM4,cbM5,cbM1,cbM2] and you perform some operation like [cbM1,cbM2, cbM3,cbM4,cbM5,cbM1,cbM2] - [cbM1,cbM2] then you will get [cbM1,cbM2, cbM3,cbM4,cbM5]
If u have [cbM1,cbM2, cbM3,cbM4,cbM5] and you perform some operation like [cbM1,cbM2, cbM3,cbM4,cbM5]-[cbM1,cbM5] you will get [cbM1,cbM2, cbM3,cbM4,cbM5]
精彩评论