C# 委托与 EventHandler 的区别

24

当陷阱发生时,我希望向任何订阅者发送一个警报消息。

我创建的代码使用代理方法 myDelegate del 运行得很好。

我的问题是:

  1. 我想知道是否最好使用 EventHandler 而不是委托? 我不确定在我的情况下委托和 EventHandler 之间的区别。

  2. notify(trapinfo t),这就是我在这里获取陷阱信息的方式。但这似乎不是一个好主意。我看了一些在线教程介绍传递委托对象;我想知道在我的情况下是否合适?我该怎么做?有什么建议吗?

非常感谢 :)

我的代码:

public class trapinfo
    {
        public string info;
        public string ip;
        public string cause;
    }

    public class trap
    {
        public delegate void myDelegate(trapinfo t);
        public myDelegate del;

        trapinfo info = new trapinfo();

        public void run()
        {
            //While(true) 
            // If a trap occurred, notify the subscriber
            for (; ; )
            {
                Thread.Sleep(500);
                foreach (myDelegate d in del.GetInvocationList())
                {
                    info.cause = "Shut Down";
                    info.ip = "192.168.0.1";
                    info.info = "Test";
                    d.Invoke(info);
                }
            }
        }
    }
    public class machine
    {
        private int _occuredtime=0;

        public trapinfo info = new trapinfo();
        public void notify(trapinfo t)
        {
            ++_occuredtime;
            info.cause = t.cause;
            info.info = t.info;
            info.ip = t.ip;
            getInfo();
        }
        public void subscribe(trap t)
        {
            t.del += new trap.myDelegate(notify);
        }
        public void getInfo()
        {
            Console.WriteLine("<Alert>: cauese/{0}, info/ {1}, ip/{2}, time/{3}",
                info.cause, info.info, info.ip,_occuredtime);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            trap t = new trap();
            machine machineA = new machine();
            machineA.subscribe(t);
            t.run();
        }
    }

更新于2013-08-12

如何使用observer/observable设计模式,在我的情况下似乎很不错(EventHandler)。

在我的情况下,一个机器订阅了一个陷阱信使。(将机器添加到调用列表中) 一旦发生陷阱,我就向所有订阅的机器发送一条消息。(调用HandleEvent来处理它)

优点:

  • 不再需要关心GetInvocationList(),只需使用(+=)(-=) 来决定向谁发送陷阱。

  • 更容易理解程序的逻辑。

我知道有几种方法可以实现它,但我希望能够分析其优缺点。

感谢您的评论和建议,这将非常有帮助!

我阅读了Matthew Watson建议的MSDN EventArgs文章。

这是我的事件版本:

public class TrapInfoEventArgs : EventArgs
{
    public int info { get; set; }
    public string  ip { get; set; }
    public string cause { get; set; }
}
public class trap
{
    public event EventHandler<TrapInfoEventArgs> TrapOccurred;

    protected virtual void OnTrapOccurred(TrapInfoEventArgs e)
    {
        EventHandler<TrapInfoEventArgs> handler = TrapOccurred;
        if (handler != null)
        {
            handler(this, e);
        }
    }


    public void run()
    {
        //While(true) 
        // If a trap occurred, notify the subscriber
        for (; ; )
        {
            Thread.Sleep(500);
            TrapInfoEventArgs args = new TrapInfoEventArgs();
            args.cause = "Shut Down";
            OnTrapOccurred(args);
        }
    }
}
public class machine
{
    public void c_TrapOccurred(object sender, TrapInfoEventArgs e)
    {
        Console.WriteLine("<Alert>: cauese/{0}, info/ {1}, ip/{2}, time/{3}",
            e.cause, e.info, e.ip, DateTime.Now.ToString());
    }
}
class Program
{
    static void Main(string[] args)
    {
        trap t = new trap();
        machine machineA = new machine();
        t.TrapOccurred += machineA.c_TrapOccurred; //notify machine A
        t.run();
    }
}

2个回答

22

事件(event)和委托(delegate)的区别是:

事件声明在委托实例上添加了一层保护。这种保护防止委托的客户端重置委托及其调用列表,并且只允许向调用列表中添加或删除目标。

参见What are the differences between delegates and events?

2)我认为,订阅者不应该自由更改委托。一个订阅者可以将其赋值为 = 而不是添加 +=。这将分配一个新的委托,因此,先前具有其调用列表的委托将丢失,并且以前的订阅者将不再被调用。因此,您应该确保使用 Event。或者,您可以将委托定义为私有,并编写附加函数来操作它,以定义自己的事件行为。

 //preventing direct assignment
 private myDelegate del ;

    public void AddCallback(myDelegate m){
        del += m;
    }

    public void RemoveCallback(myDelegate m){
        del -= m;
    }

    //or
    public static trap operator +(trap x,myDelegate m){
        x.AddCallback(m);
        return x;
    }
    public static trap operator -(trap x, myDelegate m)
    {
        x.RemoveCallback(m);
        return x;
    }

//usage  

//t.AddCallback(new trap.myDelegate(notify));
  t+=new trap.myDelegate(notify);

那么在我发布的代码中,我需要注意调用列表,是吗? - Hsu Wei Cheng
你需要提供函数来改变它,而不是直接让它可访问。面向对象的封装概念。 - qwr

10

对于您的示例,最好使用事件

  • 事件被Visual Studio表单和WPF设计师所理解,因此您可以使用IDE来订阅事件。

  • 在引发事件时,无需编写自己的foreach处理以迭代它们。

  • 事件是大多数程序员期望访问此功能的方式。

  • 如果使用委托,则使用方代码可能会以您想要防止的方式进行更改(例如重置其调用列表)。事件不允许发生这种情况。

至于您的第二个问题:使用事件,您将创建一个从EventArgs派生的类来保存数据,并在引发事件时传递该类。使用方将随后可以访问它。

详见此处: http://msdn.microsoft.com/en-us/library/system.eventargs.aspx


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接