使用强大的反射取消事件的订阅。
来源:互联网 发布:精梳羊绒知乎 编辑:程序博客网 时间:2024/06/17 15:01
using System;
using System.Collections;
using System.Reflection;
//Delegate
delegate void UpdateDelegate();
//Subject
class Subject
...{
public event UpdateDelegate UpdateHandler;
// Methods
public void Attach(UpdateDelegate ud)
...{
UpdateHandler += ud;
}
public void Detach(UpdateDelegate ud)
...{
UpdateHandler -= ud;
}
public void Notify()
...{
Console.WriteLine("-------------------------");
if (UpdateHandler != null) UpdateHandler();
Console.WriteLine("-------------------------");
}
}
// "ConcreteObserver"
class Observer
...{
// Methods
public static void Show()
...{
Console.WriteLine("Observer got an Notification!");
}
}
class AnotherObserver
...{
// Methods
public static void Show()
...{
Console.WriteLine("AnotherObserver got an Notification!");
}
}
class Instantce
...{
public void Show()
...{
Console.WriteLine("Instantce got an Notification!");
}
}
public class Client
...{
public static void Main(string[] args)
...{
Subject a = new Subject();
a.UpdateHandler += new UpdateDelegate(Observer.Show);
a.UpdateHandler += new UpdateDelegate(AnotherObserver.Show);
a.UpdateHandler += new UpdateDelegate(AnotherObserver.Show);
Instantce ins = new Instantce();
a.UpdateHandler += new UpdateDelegate(ins.Show);
Type t = a.GetType();
FieldInfo eventsPropertyInfo = t.GetField("UpdateHandler", BindingFlags.Instance | BindingFlags.NonPublic);
UpdateDelegate eventHanlderList = eventsPropertyInfo.GetValue(a) as UpdateDelegate;
Delegate[] dl = eventHanlderList.GetInvocationList();
foreach (Delegate d in dl)
...{
// look up the contents in Delegate
Console.WriteLine(d.Method.DeclaringType.ToString() + "." + d.Method.Name);
// Detach event handler
a.Detach((UpdateDelegate)d);
}
a.Notify();
}
}
using System.Collections;
using System.Reflection;
//Delegate
delegate void UpdateDelegate();
//Subject
class Subject
...{
public event UpdateDelegate UpdateHandler;
// Methods
public void Attach(UpdateDelegate ud)
...{
UpdateHandler += ud;
}
public void Detach(UpdateDelegate ud)
...{
UpdateHandler -= ud;
}
public void Notify()
...{
Console.WriteLine("-------------------------");
if (UpdateHandler != null) UpdateHandler();
Console.WriteLine("-------------------------");
}
}
// "ConcreteObserver"
class Observer
...{
// Methods
public static void Show()
...{
Console.WriteLine("Observer got an Notification!");
}
}
class AnotherObserver
...{
// Methods
public static void Show()
...{
Console.WriteLine("AnotherObserver got an Notification!");
}
}
class Instantce
...{
public void Show()
...{
Console.WriteLine("Instantce got an Notification!");
}
}
public class Client
...{
public static void Main(string[] args)
...{
Subject a = new Subject();
a.UpdateHandler += new UpdateDelegate(Observer.Show);
a.UpdateHandler += new UpdateDelegate(AnotherObserver.Show);
a.UpdateHandler += new UpdateDelegate(AnotherObserver.Show);
Instantce ins = new Instantce();
a.UpdateHandler += new UpdateDelegate(ins.Show);
Type t = a.GetType();
FieldInfo eventsPropertyInfo = t.GetField("UpdateHandler", BindingFlags.Instance | BindingFlags.NonPublic);
UpdateDelegate eventHanlderList = eventsPropertyInfo.GetValue(a) as UpdateDelegate;
Delegate[] dl = eventHanlderList.GetInvocationList();
foreach (Delegate d in dl)
...{
// look up the contents in Delegate
Console.WriteLine(d.Method.DeclaringType.ToString() + "." + d.Method.Name);
// Detach event handler
a.Detach((UpdateDelegate)d);
}
a.Notify();
}
}
- 使用强大的反射取消事件的订阅。
- 使用强大的反射取消事件的订阅。
- 使用反射取消所有事件订阅
- nodejs事件发布/订阅Emitter的使用
- 关于RXJAVA的取消订阅的理解
- Linux邮件列表的订阅与取消订阅
- QQ邮箱的订阅邮件怎么取消订阅?
- 取消事件的发生
- 事件的取消
- 取消事件的冒泡
- 事件的取消
- 强大的PHP反射机制
- EventBus源码分析(三):post方法发布事件【获取事件的所有订阅者,反射调用订阅者事件处理方法】(2.4版本)
- C# 事件 之订阅和取消订阅事件
- c#为什么要取消事件订阅
- 如何:订阅和取消订阅事件(C# 编程指南)
- 强大的js事件委托
- 取消特定的click事件
- 如何将Access数据表及数据记录完整的导入到Oracle中去
- MTK 6225 工程指令集
- 简单UDP通信
- 回顾会议需要达到什么样的目标
- WinAirCrackPack使用教程
- 使用强大的反射取消事件的订阅。
- Oracle Trigger
- WinRAR破解注册方法
- c# SQLHelper(for winForm)实现代码
- ASM问题-[INS-30511]an older ASM instance was found
- cocoa 国际化
- objective-c编程语言 第一章 对象,类和消息 第一小节 对象(objects)
- form.AutoSize
- ora-01102:cannot mount database in exclusive mode