You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现Delegate调用多个方法?多方法绑定Delegate的技术疑问

How Multicast Delegates Call Multiple Methods Through a Single "Address"

Great question! Let's demystify this—because while it looks like you're working with a single address, multicast delegates have a clever internal structure that makes this work.

The Core Truth: Multicast Delegates Aren't Single Pointers

When you create a delegate that points to one method, it's a single-cast delegate: its "address" directly points to that method's memory location. But when you bind multiple methods (using += in C#, for example), you're actually creating a multicast delegate—which isn't a single pointer at all.

Instead, it's an object (or struct, depending on the runtime) that internally maintains an invocation list: an ordered collection of pointers to each bound method. The "single address" you reference is actually a pointer to this multicast delegate object, not to any individual method.

What Happens When You Call the Multicast Delegate?

When you invoke the delegate (like myDelegate()), here's what happens under the hood:

  • The runtime (e.g., .NET's CLR) accesses the multicast delegate's invocation list.
  • It iterates through each method pointer in the list, in the order they were added.
  • It calls each method one after another using their respective memory addresses.

Key Details to Note

  • Immutability: Multicast delegates are immutable. When you use += or -= to add/remove methods, you're not modifying the original delegate—you're creating a new delegate instance with an updated invocation list. The old instance is eventually garbage collected if no references remain.
  • Exception Behavior: If one method in the list throws an unhandled exception, the runtime stops processing the rest of the list. To avoid this, you can manually iterate over the invocation list (using GetInvocationList()) and call each method with try/catch blocks:
    foreach (MyDelegate method in del.GetInvocationList())
    {
        try
        {
            method();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error in method: {ex.Message}");
        }
    }
    
  • Return Values: For delegates that return values, only the return value of the last method in the list is returned when you invoke the multicast delegate. If you need all return values, you have to iterate the list manually.

Example in C#

Here's a simple snippet to see this in action:

// Define a delegate type
public delegate void GreetingDelegate(string name);

public class Greeter
{
    public static void SayHello(string name) => Console.WriteLine($"Hello, {name}!");
    public static void SayWelcome(string name) => Console.WriteLine($"Welcome, {name}!");
    public static void SayGoodDay(string name) => Console.WriteLine($"Good day, {name}!");
}

public class Program
{
    static void Main()
    {
        // Start with one method
        GreetingDelegate greet = Greeter.SayHello;
        // Add two more methods
        greet += Greeter.SayWelcome;
        greet += Greeter.SayGoodDay;

        // Call the "single" delegate—all three methods run
        greet("Alice");
    }
}

When you run this, you'll see all three greeting messages printed in order, even though you only called greet() once.

内容的提问来源于stack exchange,提问作者Nika Varamashvili

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:39:43