2010-10-21 63 views
3

我已经创建了一个非常简单的虚拟程序来理解代表和事件。在我的下面的程序中,我简单地调用一个方法。当我调用一个方法时,有五种方法在委托和事件的帮助下自动调用。代表和事件

请看看我的程序,并让我知道我错了或正确的地方,因为这是我第一次使用代表和事件。

using System; 

namespace ConsoleApplication1 
{ 
    public delegate void MyFirstDelegate(); 

    class Test 
    { 
     public event MyFirstDelegate myFirstDelegate; 

     public void Call() 
     { 
      Console.WriteLine("Welcome in Delegate world.."); 
      if (myFirstDelegate != null) 
      { 
       myFirstDelegate(); 
      } 
     }   
    } 

    class AttachedFunction 
    { 
     public void firstAttachMethod() 
     { 
      Console.WriteLine("ONE..."); 
     } 
     public void SecondAttachMethod() 
     { 
      Console.WriteLine("TWO..."); 
     } 
     public void thirdAttachMethod() 
     { 
      Console.WriteLine("THREE..."); 
     } 
     public void fourthAttachMethod() 
     { 
      Console.WriteLine("FOUR..."); 
     } 
     public void fifthAttachMethod() 
     { 
      Console.WriteLine("FIVE..."); 
     } 
    } 

    class MyMain 
    { 
     public static void Main() 
     { 
      Test test = new Test(); 
      AttachedFunction attachedFunction = new AttachedFunction(); 
      test.myFirstDelegate += new MyFirstDelegate(attachedFunction.firstAttachMethod); 
      test.myFirstDelegate += new MyFirstDelegate(attachedFunction.SecondAttachMethod); 
      test.myFirstDelegate += new MyFirstDelegate(attachedFunction.thirdAttachMethod); 
      test.myFirstDelegate += new MyFirstDelegate(attachedFunction.fourthAttachMethod); 
      test.myFirstDelegate += new MyFirstDelegate(attachedFunction.fifthAttachMethod); 

      test.Call(); 
      Console.ReadLine(); 
     } 
    } 
} 
+0

它有什么问题吗?你没有提出问题 – Andrey 2010-10-21 16:04:45

+0

看起来像一个很好的实现如何使用代表给我。如果你愿意,你可以使用C#2.0和更高级的语法糖代替'+ = new MyFirstDelete(methodName);',只需要'+ = methodName;'。此外,匿名代表也可以随时处理。 – 2010-10-21 16:09:41

+0

@Jesse ...谢谢..我将使用+ = methodName :) – 2010-10-21 17:28:10

回答

2

活动正在使用的代表实现。这就是说按照惯例事件采取以下形式:

void EventHandler(Object sender, EventArgs args); 

EventHandler实际上是在.net中定义的代表。 EventArgs是.Net中的一个类,充当占位符来传递附加信息。如果您有其他信息,您可以创建一个从EventArgs派生并包含其他数据的属性的类;因此,你会创建自己的委托,像这样:

void MyEventHandler(Object sender, MyEventArgs args); 

微软对事件here的教程和还介绍了包括定义和引发事件here

+0

谢谢Steve..Nice Link .. :) – 2010-10-21 17:34:08

0

尝试把线

public delegate void MyFirstDelegate(); 

测试类中。

此外,使用Invoke功能上的事件,而不是,即

myFirstDelegate.Invoke(); 
+0

感谢Macfanpro。 – 2010-10-21 17:30:50

2

这是处理事件的一种常见的模式:

// define the delegate 
public delegate void CustomEventHandler(object sender, CustomEventArgs e); 

// define the event args 
public class CustomEventArgs : EventArgs 
{ 
    public int SomeValue { get; set; } 

    public CustomEventArgs(int someValue) 
    { 
     this.SomeValue = someValue; 
    } 
} 

// Define the class that is raising events 
public class SomeClass 
{ 
    // define the event 
    public event CustomEventHandler CustomEvent; 

    // method that raises the event - derived classes can override this 
    protected virtual void OnCustomEvent(CustomEventArgs e) 
    { 
     // do some stuff 
     // ... 

     // fire the event 
     if(CustomEvent != null) 
      CustomEvent(this, e); 
    } 

    public void SimulateEvent(int someValue) 
    { 
     // raise the event 
     CustomEventArgs args = new CustomEventArgs(someValue); 

     OnCustomEvent(args); 
    } 
} 

public class Main 
{ 
    public static void Main() 
    { 
     SomeClass c = new SomeClass(); 

     c.CustomEvent += SomeMethod; 
     c.SimulateEvent(10); // will cause event 
    } 

    public static void SomeMethod(object sender, CustomEventArgs e) 
    { 
     Console.WriteLine(e.SomeValue); 
    } 
} 
+0

谢谢Dismissile – 2010-10-21 17:37:40