我如何用C#制作我自己的事件?

推荐答案

下面是使用C#创建和使用事件的示例

using System;

namespace Event_Example
{
    //First we have to define a delegate that acts as a signature for the
    //function that is ultimately called when the event is triggered.
    //You will notice that the second parameter is of MyEventArgs type.
    //This object will contain information about the triggered event.
    public delegate void MyEventHandler(object source, MyEventArgs e);

    //This is a class which describes the event to the class that recieves it.
    //An EventArgs class must always derive from System.EventArgs.
    public class MyEventArgs : EventArgs
    {
        private string EventInfo;
        public MyEventArgs(string Text)
        {
            EventInfo = Text;
        }
        public string GetInfo()
        {
            return EventInfo;
        }
    }

    //This next class is the one which contains an event and triggers it
    //once an action is performed. For example, lets trigger this event
    //once a variable is incremented over a particular value. Notice the
    //event uses the MyEventHandler delegate to create a signature
    //for the called function.
    public class MyClass
    {
        public event MyEventHandler OnMaximum;
        private int i;
        private int Maximum = 10;
        public int MyValue
        {
            get
            {
                return i;
            }
            set
            {
                if(value <= Maximum)
                {
                    i = value;
                }
                else
                {
                    //To make sure we only trigger the event if a handler is present
                    //we check the event to make sure it's not null.
                    if(OnMaximum != null)
                    {
                        OnMaximum(this, new MyEventArgs("You've entered " +
                            value.ToString() +
                            ", but the maximum is " +
                            Maximum.ToString()));
                    }
                }
            }
        }
    }

    class Program
    {
        //This is the actual method that will be assigned to the event handler
        //within the above class. This is where we perform an action once the
        //event has been triggered.
        static void MaximumReached(object source, MyEventArgs e)
        {
            Console.WriteLine(e.GetInfo());
        }

        static void Main(string[] args)
        {
            //Now lets test the event contained in the above class.
            MyClass MyObject = new MyClass();
            MyObject.OnMaximum += new MyEventHandler(MaximumReached);

            for(int x = 0; x <= 15; x++)
            {
                MyObject.MyValue = x;
            }

            Console.ReadLine();
        }
    }
}

.net相关问答推荐

如何确定计时器是否正在运行?

无法加载文件或程序集 不支持操作. (来自 HRESULT 的异常:0x80131515)

单线程单元 - 无法实例化 ActiveX 控件

.NET 中的线程安全集合

如何获得友好的操作系统版本名称?

mstest.exe 在哪里?

如何在 .NET 中将字符串转换为字节数组?

Linq查询分组并 Select 第一个项目

大型 WCF Web 服务请求因 (400) HTTP 错误请求而失败

获取类型的默认构造函数的最有效方法

为什么 Interlocked.Exchange 不支持布尔类型?

为什么 WCF 中不允许方法重载?

创建多个线程并等待所有线程完成

为什么 System.Timers.Timer 能在 GC 中存活,而 System.Threading.Timer 不能?

是否可以完全用托管的 .NET 语言编写 JIT 编译器(本地代码)

获取系统中已安装的应用程序

如何修复 .NET Windows 应用程序在启动时崩溃并出现异常代码:0xE0434352?

为什么 Roslyn 中有异步状态机类(而不是 struct )?

嵌套的 Try/Catch 块是个坏主意吗?

根据条件从列表中删除项目