C#允许我们创建自定义事件访问器.
Action _custom;
public event Action Custom
{
add { _custom = (Action)Delegate.Combine( _custom, value ); }
remove { _custom = (Action)Delegate.Remove( _custom, value ); }
}
Run Code Online (Sandbox Code Playgroud)
如果未指定它们,编译器将为您创建它们.C#语言规范:
编译类似字段的事件时,编译器会自动创建存储以保存委托,并为事件创建访问器,以便向委托字段添加或删除事件处理程序.
使用dotPeek进行简化的反编译源代码public event Action Public;如下所示:
private Action Public;
public event Action Public
{
add
{
Action action = this.Public;
Action comparand;
do
{
comparand = action;
action = Interlocked.CompareExchange<Action>(
ref this.Public, comparand + value, comparand);
}
while (action != comparand);
}
remove
{
Action …Run Code Online (Sandbox Code Playgroud) 我有一个活动 Load
public delegate void OnLoad(int i);
public event OnLoad Load;
Run Code Online (Sandbox Code Playgroud)
我订阅了一个方法:
public void Go()
{
Load += (x) => { };
}
Run Code Online (Sandbox Code Playgroud)
是否可以使用反射检索此方法?怎么样?