English 中文(简体)
Removing delegates from chain
原标题:
  • 时间:2009-11-11 08:27:45
  •  标签:
  • c#
  • .net
  • clr
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;

        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]





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签