解c#中的代理和事件(一)

来源:互联网 发布:淘宝店铺全屏轮播代码 编辑:程序博客网 时间:2024/04/24 13:40
解c#中的代理和事件(一)

  突然写这篇文章,真的有点,是在做作的感觉,我想这并不是什么,难以

理解的东西,事实上,很多人都写过,而且,我保证至少不比我写的差.可是

还是觉得有必要提出来.因为要去正确的理解代理和事件是很有趣的,也是

很必要的.那么好吧,下面我就来讲讲,它们之间的关系.当然还会有些例子.

  首先我想说说有关事件.事件顾名思义当然是windows程序收到的消息.

那么我举几个有关事件的例子,如鼠标移动.按下.之类的都是.那么代理呢?

很多人都说它看上去,就是想是一个受托管的函数指针.我觉得这种说法很

正确,事实如此.在msdn上,我们经常可以看到,有关代理的"多路广播"这个词

我觉得不错,可是不好理解.但我会在下面详细讲解的.

代理:(有的书上也翻译成指代或委托.英文是这样一来的"delegate")

我想很多刚接触c#的人,都会对代理产生兴趣的.事实上也是如此,不了解它,你就没办法

来做windows程序.和传统意义上的函数指针有所不同的是,代理在c#中是一种类型,这样

它看上去,更安全也,更符合oo精神.代理实际上所做的工作就是通过引用把函数包裹起来

并使函数有一个有效的返回值.不知道我这样说是否好理解.那么我举个例子,你去建造房子.

很显然,我是在说你所做的事情.那么建造房子就是代理,它指代了,你要做的事情,可是它并

没有去做任何事情,事实上是,在建造房子这个工作里,你做了,那么结果是什么?当然是建立

一座房子.是的,建造房子就是代理,而如何建造房子则是函数应该完成的工作.而建造的是什么样

的房子,则是返回值.还记得,我曾经说过,代理是一种类型吗?呵呵..我想你应该记得,因为,

那是很新颖的,至少当时我那么认为.好吧,让我们来看看名称空间system.delagate,看见了吗?那

就代理类.

举个例子:

public delegate void getstring()//我申明了一个代理

现在我要用到它了如下;

int i=100;

getstring gs=new getstring(i.tostring);//这里我吧int的tostring方法

填入了一个代理中.看上去想构造函数.这就是常在书上看到的"名称等效的,而

不是结构等效的",我想看到这儿你还是不明白.那么,我再来一个代理

如下:

float j=0.0001;

getstring gs=new getstring(j.tostring);//瞧见了,int的tostring方法

和float的tostring方法的结构是不一样的,可是名称和类型的返回值和

参数都一样.现在,我想,你应该理解了吧.

可是,我们经常会在msdn中看到,这样的句子.单路代理和多路广播.看上去,有点不好理解.

事实上,我开始看这样的句子,是有点头痛的.那么,我想例子是最好的解说方式.

single delegate:(单路代理)

从字面上,我们可以这样来理解,这个代理只是单单代理了一个函数的工作.那么好吧,让

我们来看看它是如何工作的.下面我就来定义一个这样的代理:

public delegate bool myfun(string str,int i)

现在我再来写一个方法如下:

bool comparestrtoint(string s,int i)

<

   if(s.compareto(i.tostring())==0)

        return true;

    else

         return false;

>

这个方法完成的工作很简单对吧,只是比较字符而已.那么和代理有什么关系呢?还记得

我说的话吗?代理就是在把动词名词化.代码如下:

myfun mf=new  (comparestrtoint);

string s="10000";

int i=10000;

console.writeline("value="+mf(s,i));

输出结果:

value=true

这就是单路代理.它只代理一个.好吧,也许你想看看复杂的例子,更有趣的在后面呢,

该是讨论多路广播的时候了.

多路广播:

一个代理同时代理几个方法.就是我们前面说到的那样,你去建造房子,现在要不仅仅是

建造住宅,还的去建造花园等等其它建筑物.可是它们都是在建造房子,传递的参数也相同

返回值的类型也相同都是房屋.那么我们为什么不找一个代理人来完成这样的任务呢?把

这些事物交由他一个人来完成不是可以节省我们很多的时间和金钱.是的我们可以那样做

system.multicastdelegate 实际上在.net framework中你还可以找到这个类,多路代理

msdn上翻译成多路广播.事实上它还重载了操作符+=.其实多路广播和单路代理在使用方法

上区别不大.你可以看下面的例子.

using system;

namespace multi_castdelegate

<

  /// <summary>

  /// summary description for class1.

  /// </summary>

  class myclassdelegate

  <

    /// <summary>

    /// the main entry point for the application.

    /// </summary>

    public delegate string intdelegate(string s);

  >

>

using system;

namespace multi_castdelegate

<

  /// <summary>

  /// summary description for myimplementingclass.

  /// </summary>

  public class myclass

  <

    public myclass()

    <

    >

    public static string writestring(string s)

    <

      console.writeline("writing string");

      return "null";

    >

    public static string logstring(string s)

    <

      console.writeline("loging string"); 

      return "null";

    >

    public static string transmitstring(string s)

    <

      console.writeline("transmitting string"); 

      return "null";

    >

  >

>

the main class:

using system;

using system.threading;

namespace multi_castdelegate

<

  /// <summary>

  /// summary description for test.

  /// </summary>

  public class test

  <

    public static void main()

    <

      myclassdelegate.stringdelegate

        writer,logger,transmitter;

      myclassdelegate.stringdelegate

        mydelegate;

      writer=new

                   myclassdelegate.stringdelegate(myclass.writestring);

      /// calling writer

      writer("hello i am writer just acting like single cast");

      logger=new myclassdelegate.stringdelegate(myclass.logstring);

      ///calling logger

      logger("hello i am logger just acting like single-cast");

      transmitter=new myclassdelegate.stringdelegate(myclass.transmitstring);

      ///calling transmitter

            transmitter("hello i am transmitter just acting like single-cast"); 

      ///here mydelegate used the combine method of system.multicastdelegate

      ///and the delegates combine  

      mydelegate=(myclassdelegate.stringdelegate)system.delegate.combine(writer,logger); 

            mydelegate("used combine");

      ///here transmitter is also added using the overloaded form of combine

      mydelegate+=transmitter;

      mydelegate("using overloaded form");

      ///now using the remove method

      mydelegate=(myclassdelegate.stringdelegate)system.delegate.remove(mydelegate,writer);

            mydelegate("without writer");    

      ///overloaded remove

      mydelegate-=transmitter;

      mydelegate("without transmitter");

        system.threading.thread.sleep(2300);  

    >

  >

>

(上面的例子是在一个国外网站上找到的,觉得不错,就直接套用了.)

上面的例子重点是看那两个已经重载的操作符."-="和"+=".通过上面的例子,你可以清楚的

看到多路广播是如何一次代理多个方法的.当然你也可以删除掉那些你不想要的用"-="操作

符就可以了.( 
原创粉丝点击