博客
关于我
委托-利用GetInvocationList处理链式委托
阅读量:582 次
发布时间:2019-03-11

本文共 2591 字,大约阅读时间需要 8 分钟。

在C#中,当使用委托代理多个函数时,如果每次都使用+=来添加新的函数到同一个委托链中,可能会遇到一个问题:每次添加新的函数都会覆盖之前的委托,导致最终只能看到最后一个函数的返回值。为了正确地获取所有函数的返回值,可以使用GetInvocationList方法来遍历委托链中的所有函数。

以下是详细的解决方案:

  • 定义委托和类

    • 定义一个委托TestDelegate,其返回类型为string
    • 创建一个类DelegateClass,包含一个TestDelegate类型的属性testDelegate
  • 在Main方法中创建函数实例并添加到委托链

    • 创建四个不同的函数实例onetwothreefour,它们都实现了Say方法,返回不同的字符串。
    • 使用+=操作符将每个函数的Say方法添加到DelegateClasstestDelegate属性中。
  • 调用委托并遍历所有函数

    • DelegateClass类中实现InvokeDelegate方法。
    • InvokeDelegate方法中,检查testDelegate是否不为空。
    • 如果不为空,使用GetInvocationList方法获取委托链中的所有函数。
    • 遍历每个函数,逐个转换为TestDelegate类型并调用,获取返回值。
    • 打印每个返回值。
  • 完整代码示例

    using System;namespace GetInvocationListDemo{    public delegate string TestDelegate();    public class Program    {        static void Main(string[] args)        {            DelegateClass delegateClass = new DelegateClass();            TestMethodOne one = new TestMethodOne();            TestMethodTwo two = new TestMethodTwo();            TestMethodThree three = new TestMethodThree();            TestMethodFour four = new TestMethodFour();            delegateClass.testDelegate += one.Say;            delegateClass.testDelegate += two.Say;            delegateClass.testDelegate += three.Say;            delegateClass.testDelegate += four.Say;            delegateClass.InvokeDelegate();            Console.ReadKey();        }    }    public class DelegateClass    {        public TestDelegate testDelegate;        public void InvokeDelegate()        {            if (testDelegate != null)            {                foreach (Delegate dele in testDelegate.GetInvocationList())                {                    TestDelegate delegateClass = (TestDelegate)dele;                    string resultStr = delegateClass();                    Console.WriteLine(resultStr);                }            }        }    }    public class TestMethodOne    {        public string Say()        {            return "You called me from TestMethodOne~~~";        }    }    public class TestMethodTwo    {        public string Say()        {            return "You called me from TestMethodTwo~~~";        }    }    public class TestMethodThree    {        public string Say()        {            return "You called me from TestMethodThree~~~";        }    }    public class TestMethodFour    {        public string Say()        {            return "You called me from TestMethodFour~~~";        }    }}
  • 运行结果

    • 执行上述代码时,会依次输出四个方法的返回值:
      You called me from TestMethodOne~~~You called me from TestMethodTwo~~~You called me from TestMethodThree~~~You called me from TestMethodFour~~~
  • 通过使用GetInvocationList方法,可以确保所有添加到委托链中的函数都会被调用并获取其返回值,从而避免只保留最后一个函数的结果。

    转载地址:http://pdlvz.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现multilayer perceptron classifier多层感知器分类器算法(附完整源码)
    查看>>
    Objective-C实现multiplesThreeAndFive三或五倍数的算法 (附完整源码)
    查看>>
    Objective-C实现n body simulationn体模拟算法(附完整源码)
    查看>>
    Objective-C实现naive string search字符串搜索算法(附完整源码)
    查看>>
    Objective-C实现natural sort自然排序算法(附完整源码)
    查看>>
    Objective-C实现nested brackets嵌套括号算法(附完整源码)
    查看>>
    Objective-C实现nevilles method多项式插值算法(附完整源码)
    查看>>
    Objective-C实现newtons second law of motion牛顿第二运动定律算法(附完整源码)
    查看>>
    Objective-C实现newton_raphson牛顿拉夫森算法(附完整源码)
    查看>>
    Objective-C实现NLP中文分词(附完整源码)
    查看>>
    Objective-C实现NLP中文分词(附完整源码)
    查看>>
    Objective-C实现not gate非门算法(附完整源码)
    查看>>
    Objective-C实现number of digits解字符数算法(附完整源码)
    查看>>
    Objective-C实现NumberOfIslands岛屿的个数算法(附完整源码)
    查看>>
    Objective-C实现numerical integration数值积分算法(附完整源码)
    查看>>
    Objective-C实现n皇后问题算法(附完整源码)
    查看>>
    Objective-C实现O(E + V) 中找到 0-1-graph 中的最短路径算法(附完整源码)
    查看>>
    Objective-C实现OCR文字识别(附完整源码)
    查看>>
    Objective-C实现odd even sort奇偶排序算法(附完整源码)
    查看>>
    Objective-C实现ohms law欧姆定律算法(附完整源码)
    查看>>