【问题标题】:Difference between multicast delegate and simple delegate?多播委托和简单委托之间的区别?
【发布时间】:2014-07-07 23:15:23
【问题描述】:

我的理解是委托只是指向一个方法。

如果委托A通过多次强制转换指向方法B和方法C,为什么不能让委托A指向方法B和方法B调用方法C?

【问题讨论】:

  • 对不起,我不明白你的意思。如果只需要调用方法C,就用一个简单的委托?
  • 你能告诉我们你的最小实现吗?这样我们就在同一个页面上......

标签: c# delegates


【解决方案1】:

如果委托A通过多次强制转换指向方法B和方法C,为什么不能让委托A指向方法B和方法B调用方法C?

当然,您可以这样做。但委托的全部意义在于委托消费者不需要知道实现是什么。在具有多个目标的委托中,关键是他们不需要了解彼此。由于事件是由委托实现的,一个事件有多个订阅者是很常见的,这可能是多目标委托的主要用法。

但是,在 .NET 中,所有的委托都支持多个目标,所以它没有实际意义。

顺便说一句;在您的 A/B/C 场景中,您还可以这样做:

SomeDelegateType instance = delegate {
    B();
    C();
};

这是调用BC 的单个委托。或者你可以这样做:

SomeDelegateType instance = new SomeDelegateType(B) + new SomeDelegateType(C);

或者任何你想要的,真的。没什么大不了的。

【讨论】:

    【解决方案2】:
    delegate void Del(string s);
    
    class TestClass
    {
    static void Hello(string s)
    {
        System.Console.WriteLine("  Hello, {0}!", s);
    }
    
    static void Goodbye(string s)
    {
        System.Console.WriteLine("  Goodbye, {0}!", s);
    }
    
    static void Main()
    {
        Del a, b, c, d;
    
        // Create the delegate object a that references 
        // the method Hello:
        a = Hello;
    
        // Create the delegate object b that references 
        // the method Goodbye:
        b = Goodbye;
    
        // The two delegates, a and b, are composed to form c: 
        c = a + b;
    
        // Remove a from the composed delegate, leaving d, 
        // which calls only the method Goodbye:
        d = c - a;
    
        System.Console.WriteLine("Invoking delegate a:");
        a("A");
        System.Console.WriteLine("Invoking delegate b:");
        b("B");
        System.Console.WriteLine("Invoking delegate c:");
        c("C");
        System.Console.WriteLine("Invoking delegate d:");
        d("D");
    }
    }
     /* Output:
    Invoking delegate a:
    Hello, A!
    Invoking delegate b:
    Goodbye, B!
    Invoking delegate c:
    Hello, C!
    Goodbye, C!
    Invoking delegate d:
    Goodbye, D!
     */
    

    【讨论】:

    【解决方案3】:

    当您定义多播委托时,这意味着您可以向该委托添加任意数量的函数。并调用此多播委托将调用指向的所有函数。 从MSDN查看此示例:

    using System;
    
    // Define a custom delegate that has a string parameter and returns void. 
    delegate void CustomDel(string s);
    
    class TestClass
    {
        // Define two methods that have the same signature as CustomDel. 
        static void Hello(string s)
        {
            System.Console.WriteLine("  Hello, {0}!", s);
        }
    
        static void Goodbye(string s)
        {
            System.Console.WriteLine("  Goodbye, {0}!", s);
        }
    
        static void Main()
        {
            // Declare instances of the custom delegate.
            CustomDel hiDel, byeDel, multiDel, multiMinusHiDel;
    
            // In this example, you can omit the custom delegate if you  
            // want to and use Action<string> instead. 
            //Action<string> hiDel, byeDel, multiDel, multiMinusHiDel; 
    
            // Create the delegate object hiDel that references the 
            // method Hello.
            hiDel = Hello;
    
            // Create the delegate object byeDel that references the 
            // method Goodbye.
            byeDel = Goodbye;
    
            // The two delegates, hiDel and byeDel, are combined to  
            // form multiDel. 
            multiDel = hiDel + byeDel;
    
            // Remove hiDel from the multicast delegate, leaving byeDel, 
            // which calls only the method Goodbye.
            multiMinusHiDel = multiDel - hiDel;
    
            Console.WriteLine("Invoking delegate hiDel:");
            hiDel("A");
            Console.WriteLine("Invoking delegate byeDel:");
            byeDel("B");
            Console.WriteLine("Invoking delegate multiDel:");
            multiDel("C");
            Console.WriteLine("Invoking delegate multiMinusHiDel:");
            multiMinusHiDel("D");
        }
    }
    
    /* Output:
    Invoking delegate hiDel:
      Hello, A!
    Invoking delegate byeDel:
      Goodbye, B!
    Invoking delegate multiDel:
      Hello, C!
      Goodbye, C!
    Invoking delegate multiMinusHiDel:
      Goodbye, D!
    */`
    

    希望对你有帮助。

    【讨论】:

    • 另一个用户提供了相同的答案。
    猜你喜欢
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    相关资源
    最近更新 更多