C#事件获取返回值

From: http://www.xuebuyuan.com/841530.html

在讲如何获取多个返回值之前先来看一个小例子:

private void button1_Click(object sender, EventArgs e)
{
  int Number = 200; //设置Number的值为 200。
  Publishser pub = new Publishser(); //实例化Publishser
  Subscriber1 sub1 = new Subscriber1(); //实例化Subscriber1
  Subscriber2 sub2 = new Subscriber2(); //实例化Subscriber2
  Subscriber3 sub3=new Subscriber3(); //实例化Subscriber3
  pub.NumberChanged += sub1.OnNumberChanged; //将方法绑定到委托
  pub.NumberChanged += sub2.OnNumberChanged;
  pub.NumberChanged += sub3.OnNumberChanged;
  pub.DoComething(Number); //调用DoComething
}

class Publishser
{
  public delegate int DemoEventHandler(int num); //定义委托,可代表的方法类型是整形
  public event DemoEventHandler NumberChanged; //申明事件
  public void DoComething(int temp)
  {
    if (NumberChanged != null)
    {
      temp=NumberChanged(temp);
    }
    MessageBox.Show(temp.ToString());
  }
}

class Subscriber1
{
  public int OnNumberChanged(int num)
  {
    MessageBox.Show(“调用了Subscriber1类,num值为:”+ num);
    return num + 100; ;
  }
}

class Subscriber2
{
  public int OnNumberChanged(int num)
  {
    MessageBox.Show(“调用了Subscriber2类 num值为:”+num);
    return num+100;
  }
}

class Subscriber3
{
  public int OnNumberChanged(int num)
  {
    MessageBox.Show(“调用了Subcriber3类,num值为:”+num);
    return num+100;
  }
}

我以为得到的结果是:

调用了Subscriber1类,num值为:200

调用了Subscriber2类,num值为:300

调用了Subscriber3类,num值为:400

500

但是程序运行完之后结果却不是我所想的,结果是:

调用了Subscriber1类,num值为:200

调用了Subscriber2类,num值为:200

调用了Subscriber3类,num值为:200

300

发现问题之后我用逐语句运行的方式调试程序,我发现当NumberChanged != null 第一次调用了Subscriber1之后并没有返回到Publishser类,也没有执行Subscriber1的 return,而是直接调用了Subscriber2,temp的值依旧是刚开始的200。执行完之后调用Subscriber3,只有当Subscriber3执行完了之后才返回到Publishser 这个时候temp的值等于300。(现在我也不知道为什么会这样,但是我在网上学习了一种可以获取多个返回值的方法!)

—————————————————————————————————————————————————————————————————

获取多个返回值:

private void button1_Click(object sender, EventArgs e)
{
  int Number = 200;
  Publishser pub = new Publishser();
  Subscriber1 sub1 = new Subscriber1();
  Subscriber2 sub2 = new Subscriber2();
  Subscriber3 sub3=new Subscriber3();
  pub.NumberChanged += sub1.OnNumberChanged;
  pub.NumberChanged += sub2.OnNumberChanged;
  pub.NumberChanged += sub3.OnNumberChanged;
  pub.DoComething(Number);
}

class Publishser
{
  public delegate int DemoEventHandler(int num);
  public event DemoEventHandler NumberChanged;
  public void DoComething(int temp)
  {
    if (NumberChanged != null)
    {
      Delegate[] delArray = NumberChanged.GetInvocationList();
      foreach (Delegate del in delArray)
      {
        DemoEventHandler method = (DemoEventHandler)del;
        temp = method(temp);
      }
    }
    MessageBox.Show(temp.ToString());
  }
}

class Subscriber1
{
  public int OnNumberChanged(int num)
  {
    MessageBox.Show(“调用了Subscriber1类,num值为:”+ num);
    return num + 100; ;
  }
}

class Subscriber2
{
  public int OnNumberChanged(int num)
  {
    MessageBox.Show(“调用了Subscriber2类 num值为:”+num);
    return num+100;
  }
}

class Subscriber3
{
  public int OnNumberChanged(int num)
  {
    MessageBox.Show(“调用了Subcriber3类,num值为:”+num);
    return num+100;
  }
}

运行得到的结果是:

调用了Subscriber1类,num值为:200

调用了Subscriber2类,num值为:300

调用了Subscriber3类,num值为:400

500

现在就是我想到的结果了,利用Delegate 基类的GetInvocationList()静态方法,可以得到委托链表,随后遍历委托链表依次执行,就可以得到每个方法的返回值了。

 

 

出处:https://dandelioncloud.cn/article/details/1536139037803134978

posted on 2022-11-24 09:10  jack_Meng  阅读(1072)  评论(0编辑  收藏  举报

导航