【温故知新】c#事件event
从上一篇文章【温故知新】C#委托delegate可知,委托delegate和事件Event非常的相似,区别就是event关键字,给delegate穿上了个“马甲”。
让我们来看官方定义:
类或对象可以通过事件向其他类或对象通知发生的相关事情。 发送(或引发)事件的类称为“发行者”,接收(或处理)事件的类称为“订户”。
event 关键字用于在发行者类中声明事件。
定义非常明确,通过事件向其他类或对象通知发生的相关事情,用来实现的观察者模式。
还是通过之前的代码例子,看看声明delegate和event
//声明一个委托类型,通知家长 public delegate void NotifyDelegate(string msg); //老师被吩咐了1个委托 //声明委托:在发现早恋时时通知家长 private NotifyDelegate NotifyStudentLove; //声明事件,如果发现学生早恋! 就要通知那些订阅了这个事件的家长。 public event NotifyDelegate FindStudentLove;
让我们通过IL DASM来看看编译之后事件event的真正面目~
.event Delegate.Teacher/NotifyDelegate FindStudentLove { .addon instance void Delegate.Teacher::add_FindStudentLove(class Delegate.Teacher/NotifyDelegate) .removeon instance void Delegate.Teacher::remove_FindStudentLove(class Delegate.Teacher/NotifyDelegate) } // end of event Teacher::FindStudentLove
.method public hidebysig specialname instance void add_FindStudentLove(class Delegate.Teacher/NotifyDelegate ‘value‘) cil managed { // 代码大小 48 (0x30) .maxstack 3 .locals init (class Delegate.Teacher/NotifyDelegate V_0, class Delegate.Teacher/NotifyDelegate V_1, class Delegate.Teacher/NotifyDelegate V_2, bool V_3) IL_0000: ldarg.0 IL_0001: ldfld class Delegate.Teacher/NotifyDelegate Delegate.Teacher::FindStudentLove IL_0006: stloc.0 IL_0007: ldloc.0 IL_0008: stloc.1 IL_0009: ldloc.1 IL_000a: ldarg.1 IL_000b: call class [mscorlib]System.Delegate [mscorlib]System.Delegate::Combine(class [mscorlib]System.Delegate, class [mscorlib]System.Delegate) IL_0010: castclass Delegate.Teacher/NotifyDelegate IL_0015: stloc.2 IL_0016: ldarg.0 IL_0017: ldflda class Delegate.Teacher/NotifyDelegate Delegate.Teacher::FindStudentLove IL_001c: ldloc.2 IL_001d: ldloc.1 IL_001e: call !!0 [mscorlib]System.Threading.Interlocked::CompareExchange<class Delegate.Teacher/NotifyDelegate>(!!0&, !!0, !!0) IL_0023: stloc.0 IL_0024: ldloc.0 IL_0025: ldloc.1 IL_0026: ceq IL_0028: ldc.i4.0 IL_0029: ceq IL_002b: stloc.3 IL_002c: ldloc.3 IL_002d: brtrue.s IL_0007 IL_002f: ret } // end of method Teacher::add_FindStudentLove
.method public hidebysig specialname instance void remove_FindStudentLove(class Delegate.Teacher/NotifyDelegate ‘value‘) cil managed { // 代码大小 48 (0x30) .maxstack 3 .locals init (class Delegate.Teacher/NotifyDelegate V_0, class Delegate.Teacher/NotifyDelegate V_1, class Delegate.Teacher/NotifyDelegate V_2, bool V_3) IL_0000: ldarg.0 IL_0001: ldfld class Delegate.Teacher/NotifyDelegate Delegate.Teacher::FindStudentLove IL_0006: stloc.0 IL_0007: ldloc.0 IL_0008: stloc.1 IL_0009: ldloc.1 IL_000a: ldarg.1 IL_000b: call class [mscorlib]System.Delegate [mscorlib]System.Delegate::Remove(class [mscorlib]System.Delegate, class [mscorlib]System.Delegate) IL_0010: castclass Delegate.Teacher/NotifyDelegate IL_0015: stloc.2 IL_0016: ldarg.0 IL_0017: ldflda class Delegate.Teacher/NotifyDelegate Delegate.Teacher::FindStudentLove IL_001c: ldloc.2 IL_001d: ldloc.1 IL_001e: call !!0 [mscorlib]System.Threading.Interlocked::CompareExchange<class Delegate.Teacher/NotifyDelegate>(!!0&, !!0, !!0) IL_0023: stloc.0 IL_0024: ldloc.0 IL_0025: ldloc.1 IL_0026: ceq IL_0028: ldc.i4.0 IL_0029: ceq IL_002b: stloc.3 IL_002c: ldloc.3 IL_002d: brtrue.s IL_0007 IL_002f: ret } // end of method Teacher::remove_FindStudentLove
实际上编译器会帮你生成如下类似代码:
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70645.html