簡體   English   中英

如何公開在另一個 class 中聲明的 C# 操作

[英]How can I expose a C# Action declared in another class

我想公開在公共 class 的內部 class 中聲明的操作

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 }

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

找到了

public event Action OnEvent
    {
        add { a.OnEvent += value; }
        remove { a.OnEvent -= value; }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM