我想公开一个在公共类的内部类中声明的动作
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 }
但我想使用 += & -= 语法