惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
GbyAI
GbyAI
IT之家
IT之家
V
Visual Studio Blog
J
Java Code Geeks
A
About on SuperTechFans
A
Arctic Wolf
Google Online Security Blog
Google Online Security Blog
博客园 - 【当耐特】
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
W
WeLiveSecurity
L
LINUX DO - 热门话题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Martin Fowler
Martin Fowler
N
News and Events Feed by Topic
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
小众软件
小众软件
Scott Helme
Scott Helme
T
Tailwind CSS Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Palo Alto Networks Blog
罗磊的独立博客
V2EX - 技术
V2EX - 技术
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 叶小钗
宝玉的分享
宝玉的分享
K
Kaspersky official blog
S
Securelist
Cyberwarzone
Cyberwarzone
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
S
Schneier on Security
Security Latest
Security Latest
腾讯CDC
SecWiki News
SecWiki News
I
InfoQ
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
The Blog of Author Tim Ferriss
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
The Last Watchdog
The Last Watchdog
C
Check Point Blog
Hacker News: Ask HN
Hacker News: Ask HN
S
Secure Thoughts
博客园_首页
Stack Overflow Blog
Stack Overflow Blog

博客园 - 宽田

SQLite在.NET中自适应32位/64位系统 OWIN是什么? 好的用户界面-界面设计的一些技巧 Android模拟器连接本李服务器localhost win7 64位 VS2010调试提示“ORA-12154: TNS: 无法解析指定的连接标识符”的解决方法 Js 验证中文字符长度 Javascript技巧 解释器模式(interpreter): 访问者模式(Visitor) 职责链模式(Chain of Responsibility) Web Service 中返回DataSet结果大小改进 通用数据链接(UDL)的用法 ORACLE WITH AS 用法 Oracle REGEXP_INSTR 用法 错误:200, Stream not found, NetStream.Play.StreamNotFound, clip: '[Clip] GetCallbackEventReference(客户端调用服务器端)的用法 wcf Svcutil用法 IIS 增加net.tcp绑定 客户端字符集NLS_LANG
C# 实现自定义事件
宽田 · 2012-06-15 · via 博客园 - 宽田

代码中实现了三个自定义事件,分别为自定义事件、自定义事件及自定义参数、使用Action自定义事件。

using System;

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

namespace AppMain
{
    /// <summary>
    
/// 入口
    
/// </summary>
    public class RunEventDemo
    {
        #region 用Action定义事件测试
        public void RunProgramThree()
        {
            EventDemoThree edth = new EventDemoThree();
            edth.CustomerEvent += new Action<object, EventArgs>(edth_CustomerEvent);
            //触发事件
            edth.OnCustomerEvent();
        }

        void edth_CustomerEvent(object obj, EventArgs e)
        {
            Console.WriteLine("已调用Actiion执行的事件");
        }
        #endregion

        #region 使用自定义事件参数事件测试
        public void RunProgramTwo()
        {
            EventDemoTwo edt = new EventDemoTwo();
            edt.CustomerEvent += new EventDemoTwo.CustomerEventHander(edt_CustomerEvent);

            //创建事件参数
            CustomerEventArgs cea = new CustomerEventArgs();
            cea.CustomerMsg = " test";

            //触发事件
            edt.OnCustomerEvent(cea);
        }

        void edt_CustomerEvent(object sender, CustomerEventArgs e)
        {
            Console.WriteLine("你传入的参数值为:" + e.CustomerMsg);
        }

        #endregion

        #region 无参的自定义事件测试
        public void RunProgramOne()
        {
            //事件接收者
            EventDemoOne ed = new EventDemoOne();
            //4.注册事件处理程序
            ed.customerEvent += new EventDemoOne.CustomerEventHander(RunDemo_customerEvent);
            //6.调用事件
            ed.OnEvent();
        }
        //5.编写事件处理程序
        public void RunDemo_customerEvent(object sender, EventArgs e)
        {
            Console.WriteLine("test");
        }
        #endregion
    }

#region无参的自定义事件

    /// <summary>
    
/// 无参的自定义事件
    
/// </summary>
    public class EventDemoOne
    {
        //1.声明关于事件的委托;
        public delegate void CustomerEventHander(object sender, EventArgs e);
        //2.声明事件;   
        public event CustomerEventHander customerEvent;
        //3.编写引发事件的函数;
        public void OnEvent()
        {
            if (this.customerEvent != null)
            {
                this.customerEvent(thisnew EventArgs());
            }
        }
    }
    #endregion

#region自定义事件,使用自定义事件参数
    /// <summary>
    
/// 自定义事件参数
    
/// </summary>
    public class CustomerEventArgs : EventArgs
    {
        public string CustomerMsg { getset; }
    }

    public class EventDemoTwo
    {
        public delegate void CustomerEventHander(object sender, CustomerEventArgs e);
        public event CustomerEventHander CustomerEvent;
        //3.编写引发事件的函数,注意多了个参数;
        public void OnCustomerEvent(CustomerEventArgs e)
        {
            this.CustomerEvent(this, e);
        }
    }
    #endregion

#region使用Action自定义事件
    public class EventDemoThree
    {
        public event Action<object, EventArgs> CustomerEvent;
        public void OnCustomerEvent()
        {
            this.CustomerEvent(thisnew EventArgs());
        }
    }
    #endregion

}