代表和活动

时间:2010-10-21 15:53:23

标签: c# delegates

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

请看看我的节目并告诉我我错在哪里或者正确,因为这是我第一次使用代表和活动。

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();
        }
    }
}

3 个答案:

答案 0 :(得分:2)

使用Delegates实现事件。按惯例说,事件采取以下形式:

 void EventHandler(Object sender, EventArgs args);

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

void MyEventHandler(Object sender, MyEventArgs args);

Microsoft提供了有关事件here的教程,还介绍了定义和引发事件here

答案 1 :(得分: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);
    }
}

答案 2 :(得分:0)

尝试放行

public delegate void MyFirstDelegate();

在Test类中。

此外,请在事件上使用Invoke函数,即

myFirstDelegate.Invoke();