C#委托和事件的调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class EventTest
    {
        public delegate void MyFunc(object sender, EventArgs e);

        public event MyFunc myFunc;
        public MyEvent myEvent;

        public EventTest()
        {
            myEvent = new MyEvent();
            myFunc += new MyFunc(myEvent.MyEventFunc);
        }

        protected void OnMyEvent(EventArgs e)
        {
            myFunc(this, e);
        }



        static void Main(string[] args)
        {
            EventTest test = new EventTest();
            EventArgs e = new EventArgs();

            test.OnMyEvent(e);
        }
    }


    public class MyEvent
    {
        public void MyEventFunc(object sender, EventArgs e)
        {
            Console.WriteLine("this is my event");
        }
    }
}

 

posted on 2013-04-19 09:03  bin斌  阅读(149)  评论(0编辑  收藏  举报

导航