相关疑难解决方法(0)

编译器生成的事件的支持字段是否始终保证使用与事件相同的名称?

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)

c# compiler-construction reflection events delegates

9
推荐指数
3
解决办法
1454
查看次数

如何检索事件的所有方法?

我有一个活动 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)

是否可以使用反射检索此方法?怎么样?

.net c# reflection

1
推荐指数
1
解决办法
3892
查看次数

标签 统计

c# ×2

reflection ×2

.net ×1

compiler-construction ×1

delegates ×1

events ×1