Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3298647
  • 博文数量: 266
  • 博客积分: 3081
  • 博客等级: 中校
  • 技术积分: 2640
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-04 10:35
个人简介

没什么好介绍的!穷屌丝一个~

文章分类

全部博文(266)

文章存档

2021年(3)

2020年(1)

2019年(2)

2016年(5)

2015年(1)

2014年(1)

2011年(9)

2010年(16)

2009年(31)

2008年(58)

2007年(111)

2006年(2)

2005年(26)

我的朋友

分类: C/C++

2008-09-15 15:23:09

1.clsTestEvent.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace 类的事件
{
    public delegate void TestEventDelegate(TestEventArgs e); //object sender,

    class clsTestEvent
    {
        private static int x;

        public static event TestEventDelegate TestEvent;
        private static void RaiseTestEvent(TestEventArgs e)
        {
            Debug.Print("发出了事件通知");
            //TestEventDelegate temp = TestEvent;

            if (TestEvent != null)
            {
                TestEvent(e); //this,

            }

        }

        public static int X
        {
            get { return x; }
            set
            {
                TestEventArgs e=new TestEventArgs();
                e.clsInfo="123";
                RaiseTestEvent(e);
                x = value;
            }
        }
    }

//使用EventArgs的派生类传递自己的参数
    public class TestEventArgs : EventArgs
    {
        private string m_clsInfo;

        public string clsInfo
        {
            get { return m_clsInfo; }
            set { m_clsInfo = value;}
        }
    }
}


2.Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace 类的事件
{
    class Program
    {
        static void Main(string[] args)
        {
            //

            System.Console.WriteLine("hehe");
            //clsTestEvent temp = new clsTestEvent();

            //temp.TestEvent +=new TestEventDelegate(temp_TestEvent);

            //temp.X = 1;

            //静态成员用法

            clsTestEvent.TestEvent += new TestEventDelegate(temp_TestEvent);
            clsTestEvent.X = 1;
            System.Console.ReadKey();
        }
        private static void temp_TestEvent(TestEventArgs e) //object sender,

        {
            System.Console.WriteLine("Event received from ");
            Console.WriteLine(e.clsInfo);
            //System.Console.WriteLine(sender.ToString());

        }

    }
}

阅读(1137) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~