CLR 委托封装 add和remove

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace DelegeteNotes
{
    public class NewMailEventArgs : EventArgs
    {
        private readonly String m_from, m_to, m_subject;
        public String From { get { return m_from; } }
        public String To { get { return m_to; } }
        public String Subject { get { return m_subject; } }
    }
    internal class MailManager
    {

        public event Action _onMouseMove;
        public event Action OnMouseMove
        {
            add
            {
                lock (obj)
                {
                    _onMouseMove += value;
                }
            }
            remove
            {
                lock (obj)
                {
                    _onMouseMove -= value;
                }
            }
        }
        public void Notify()
        {
            if (_onMouseMove != null)
            {
                _onMouseMove();
            }

        }

        private event EventHandler m_newMail;
        private static readonly Object obj = new Object();
        private event EventHandler NewMail
        {
            add
            {
                lock(obj)
                {
                    m_newMail += value;
                }
            }
            remove
            {
                lock (obj)
                {
                    m_newMail -= value;
                }
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine(obj.GetHashCode());
            MailManager m=new MailManager();
            m.OnMouseMove += () => { Console.WriteLine("woca"); };
            m.Notify();
            m._onMouseMove();
        }

    }
}

你可能感兴趣的:(C#)