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

推荐订阅源

WordPress大学
WordPress大学
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
GbyAI
GbyAI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园_首页
D
Docker
S
Security @ Cisco Blogs
K
Kaspersky official blog
爱范儿
爱范儿
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
V
V2EX
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Troy Hunt's Blog
Cloudbric
Cloudbric
博客园 - 三生石上(FineUI控件)
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Hacker News
The Hacker News
美团技术团队
S
SegmentFault 最新的问题
L
Lohrmann on Cybersecurity
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
宝玉的分享
宝玉的分享
The Last Watchdog
The Last Watchdog
Y
Y Combinator Blog
M
MIT News - Artificial intelligence
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Martin Fowler
Martin Fowler
Google Online Security Blog
Google Online Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tor Project blog
Vercel News
Vercel News
The Cloudflare Blog
G
Google Developers Blog
T
Threat Research - Cisco Blogs
AI
AI
Stack Overflow Blog
Stack Overflow Blog
I
InfoQ
Scott Helme
Scott Helme
S
Schneier on Security
大猫的无限游戏
大猫的无限游戏
The GitHub Blog
The GitHub Blog
S
Securelist
IT之家
IT之家
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - cowbird

云从哪里来 闲谈“云计算” 麦格纳软件公司绩效考核信息化管理解决方案 工作回顾(二) 工作回顾(一) 小型软件企业的绩效考核(5) 小型软件企业的绩效考核(4) 小型软件公司的绩效考核(3) 小型软件公司的绩效考核(2) 小型软件公司的绩效考核(1) Licenses.licx 总是签出,影响多人开发的解决方法 “找不到指定的模块”解决Reporting Service2000订阅邮件的问题 使用enterprise library2.0的loging application block 心得 我理解的委托(delegate) 很好的Ajax教程,有源代码 有感BigScreen项目(2005年10月31日起) 转:经理是做什么的? 三十述怀 解决windows2003自动更新错误0x80244019的问题
我理解的事件(Event)
cowbird · 2006-02-12 · via 博客园 - cowbird

       最近了解了委托概念,接着看了组合委托和事件。发现自己忽略了很多基础的东西。这篇文章接着上篇《我理解的委托(delegate)》 http://cowbird.cnblogs.com/archive/2006/02/10/328394.html)。供和我一样的初学者学习。

       委托还有组合委托的概念,上篇文章中写到两个方法,如下:
使用方法1 this.myFunction(new 被附体的代理申明(准备附体的方法1));
使用方法2 this.myFunction(new 被附体的代理申明(准备附体的方法2));
再补充一个,使用方法3 this.myFunction(new 被附体的代理申明(准备附体的方法1)
     + new 被附体的代理申明(准备附体的方法2)
     - new 被附体的代理申明(准备附体的方法2));
其实,方法3=方法1,那就是委托组合的概念。也许你现在和我一样想不出它的使用场景。

 
 看了委托,自然遇到自定义事件(自定义事件大多使用委托的方法)。它们的区别是本来是委托一个方法,现在是委托一个事件。
代码的区别就是本来的使用方法1 this.myFunction(new 被附体的代理申明(准备附体的方法1));
而现在的使用方法是 某个类.myEvent += new 被附体的代理申明(准备附体的事件1);
另外,使用场景往往是要把自定义事件插到原来的某些事件里,所以要遇到继承类和重载方法(override),但这个不难理解。

准备一
 1)主体内申明
  public delegate void 关于事件的代理声明(object sender,EventArgs e);
 2) 主体内申明使用代理的方法
  //这里继承类
  public class MyArrayList :ArrayList 
  {
   public event 关于事件的代理声明 myEvent;
   //这里重载类
   public override int Add(object value)
   {
    int i = base.Add(value);
    myEvent(this, EventArgs.Empty);
    return i;
   }
  }
 准备二
 1) 主体外写准备附体的方法,上面申明的参数和返回值一致
  private void 准备附体的事件1(object sender, EventArgs e)
  {

  }
你也可以多申明几个
  private void 准备附体的事件2(object sender, EventArgs e)
  {

  }

准备三
myArrayList.myEvent += new 被附体的代理申明(准备附体的事件1);

        到这时候,委托组合开始有意义了,应该有很多场景需要多个事件依此执行。
比如:myArrayList.myEvent += new 被附体的代理申明(准备附体的事件1 +准备附体的事件2);


事件教程参考
http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/csref/html/vcwlkeventstutorial.asp