小编Igo*_*gor的帖子

如何通过引用传递System.Action?

I'm passing a System.Action as a parameter to a method which does some lengthy operation and want to add more stuff to the invocation list after the Action has been passed:

class Class1
{
    private System.Action onDoneCallback;
    void StartAsyncOperation(System.Action onDoneCallback)
    {
        this.onDoneCallback = onDoneCallback;
        // do lengthy stuff

    }

    void MuchLater()
    {
         this.onDoneCallBack?.Invoke();
    }
}

class Class2
{
    public System.Action action;

    void DoStuff()
    {
        action += () => print ("a");
        new Class1().StartAsyncOperation(action);
    }

    {
        // ... much later in another …
Run Code Online (Sandbox Code Playgroud)

c# delegates action reference ref

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

标签 统计

action ×1

c# ×1

delegates ×1

ref ×1

reference ×1