What is the difference between a delegate instance

2019-03-19 11:19发布

问题:

I thought that a delegate instance was interchangeable with a function instance.

Take the following code:

delegate int AddDelegate(int a, int b);

AddDelegate DelegateInstance;

public void DoStuff()
{
    //I can call this without a delegate "instance":
    MethodThatTakesAdd(Add);

    //I can also call it WITH a delegate "instance"
    DelegateInstance = Add;
    MethodThatTakesAdd(DelegateInstance);
}

public int Add(int a, int b)
{
    return a + b;
}

public void MethodThatTakesAdd(AddDelegate addFunction)
{
    Console.WriteLine(addFunction(1, 2).ToString());
}

Both ways of calling it APPEAR to be equivalent, and if you're using only C#, you'll never see the difference (at least I have not up to this point). However, I was recently unmanaged code that was calling back into this managed code, they are treated differently. For example, in one scenario, I to get the error "A callback was made on a garbage collected delegate" if I use the function directly as a callback (even though my object instance is kept around). Using the "delegate instance" fixes the problem.

Is there someone out there that knows what the difference is?

回答1:

Terminology Corretion: Instead of method pointer, the more appropriate term is method group.

In terms of functionality the two statements are equivalent. That is that they produce almost the same IL. The difference is where the delegate value is stored.

In the first case you pass the method group Add to MethodThatTakesAdd directly. This causes a temporary delegate value to be created and then passed to MethodThatTakesAdd. This delegate value is subject to garbage collection the moment the MethodThatTakesAdd returns since it does not store the value.

In the second case you assigned the delegate to a field on the outer instance. This will typically increase the lifetime of the delegate and hence reduce the chance it's garbage collected during your pinvoke call.



回答2:

Delegates are classes that are callable, and have similar behavior to function pointers. The delegate internally stores the address of the function to call (i.e. the function pointer), but also provides other functionality such as multi-casting and storing an invocation list; you can essentially invoke many functions of the same signature with one delegate instance as follows.

public void DoStuff()
{
    DelegateInstance += Add;
    DelegateInstance += AnotherAdd;
    DelegateInstance += YetAnotherAdd;

    // Invoke Add(100, 200), AnotherAdd(100, 200), and YetAnotherAdd(100, 200)
    DelegateInstance(100, 200);
}

Regarding your note about the equivalence of MethodThatTakesAdd(Add) and MethodThatTakesAdd(DelegateInstance), if you look at the MSIL that the C# compiler generates for the line MethodThatTakesAdd(Add), you will notice that the compiler is creating a delegate and wrapping the Add() method for you.



回答3:

While delegates provide synonymous functionality in C# as function pointers in C or C++, there are significant differences. Key among these is that a delegate is a class, not a pointer.

In short, casting a delegate to a pointer isn't going to give you a reference to a function or method, and as such it can't be used to call a method by reference from unmanaged code.