0

我想公开一个在公共类的内部类中声明的动作

internal class A
{ 
     public Action OnEvent { get; set; }
}

public class B
{
     private A a = new A();

     public Action OnEvent 
     {
         get => a.OnEvent;
         set => a.OnEvent += value;    <- this is not correct

     }
}

我正在寻找一个允许编写如下代码的属性 getter/setter:

var b = new B();
b.OnEvent += DoSomething;  // this should add DoSomething to B.a.OnEvent
...
b.OnEvent -= DoSomething;  // this should remove DoSomething from B.a.OnEvent

编辑一种解决方案是添加

void Add(Action handler) 
{ a.OnEvent += handler}

void Remove(Action handler) 
{ a.OnEvent -= handler }

但我想使用 += & -= 语法

4

1 回答 1

1

找到了

public event Action OnEvent
    {
        add { a.OnEvent += value; }
        remove { a.OnEvent -= value; }
    }
于 2020-05-19T20:54:02.350 回答