为何不能在子类或外部发布C#事件2013-11-13背景一个朋友问了一个问题:“为什么不能在子类或外部发布C#事件?”,我说我不知道,要看看生产的IL代码,下面我们看看。测试代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EventStudy{class Program{static void Main(string[] args){}}class Base{private Action _testEventB;public event Action TestEventA;public event Action TestEventB{add{_testEventB += value;}remove{_testEventB -= value;}}protected void OnTestEventA(){var testEventA = this.TestEventA;testEventA();}protected void OnTestEventB(){var testEventB = _testEventB;testEventB();}}class Child : Base{public void Do(){//this.TestEventA();不能这样访问}}}