我如何用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相关问答推荐

Long.MaxValue从Single到Long的转换导致溢出异常

API响应返回null错误. NET MAUI

从没有流的EventStore中读取流不存在异常

Powershell机器令牌组

SLN配置文件:映射问题

在 Rx 中,处理线程安全是否是消费者(IObserver)的责任?

如何在没有抽象基类的情况下强制覆盖后代中的方法?

如何在 EF 代码优先中禁用链接表的级联删除?

如何在 C# 中直接执行 SQL 查询?

新的 netstandardapp 和 netcoreapp TFM 有什么区别?

如何授予所有用户对我的应用程序创建的文件的完全权限?

String.Split 仅在 C# 中的第一个分隔符上?

自定义属性的构造函数何时运行?

在同一解决方案中引用 2 个不同版本的 log4net

.Net 正则表达式:单词字符 \w 是什么?

如何以编程方式删除 WebClient 中的 2 个连接限制

说明 C# 中 volatile 关键字的用法

System.Array.CopyTo() 和 System.Array.Clone() 之间的区别

浮动与双重性能

如何在 ASP.NET MVC 中重定向到动态登录 URL