【问题标题】:C# pass action with parametersC# 带参数传递动作
【发布时间】:2017-07-09 20:42:30
【问题描述】:

今天我正在寻找一些带有 ilspy 的项目,我不明白如何像这样使用 Actions,在一个类中调用此方法

    public void Login(Action success, Action<bool> failure)
    {
        if (!FB.IsInitialized)
        {
            Debug.Log("[FB] Not yet initialized. Will init again!");
            FB.Init(new InitDelegate(this.OnInitComplete), null, null);
            return;
        }
        new LoginWithReadPermissions(this.READ_PERMISSIONS, delegate
        {
            ServiceLocator.GetDB().SetBool("facebookBound", true, false);
            this.OnLoginCompleted(success, failure);
        }, delegate
        {
            failure(false);
        });
    }

这是上面方法调用的另一个类

    public class LoginWithReadPermissions
{
    private readonly Action _failureCallback;

    private readonly Action _successCallback;

    public LoginWithReadPermissions(string[] scope, Action success, Action failure)
    {
        this._successCallback = success;
        this._failureCallback = failure;
        FB.LogInWithReadPermissions(scope, new FacebookDelegate<ILoginResult>(this.AuthCallback));
    }

    private void AuthCallback(ILoginResult result)
    {
        if (result.Error == null && FB.IsLoggedIn)
        {
            this._successCallback();
        }
        else
        {
            this._failureCallback();
        }
    }
}

有人可以解释一下那里发生了什么吗?我从未遇到过这种 Action 用法。感谢您的回复。

【问题讨论】:

标签: c# callback delegates action ilspy


【解决方案1】:
public void Login(Action success, Action<bool> failure)
{
    if (!FB.IsInitialized)
    {
        Debug.Log("[FB] Not yet initialized. Will init again!");
        FB.Init(new InitDelegate(this.OnInitComplete), null, null);
        return;
    }
    new LoginWithReadPermissions(this.READ_PERMISSIONS, delegate
    {
        ServiceLocator.GetDB().SetBool("facebookBound", true, false);
        this.OnLoginCompleted(success, failure);
    }, delegate
    {
        failure(false);
    });
}

上面的代码只是这个的简写:

public void Login(Action success, Action<bool> failure)
    {
        Action successAction = () =>
        {
            ServiceLocator.GetDB().SetBool("facebookBound", true, false);
            this.OnLoginCompleted(success, failure);
        };

        Action failureAction = () =>
        {
            failure(false);
        };

        if (!FB.IsInitialized)
        {
            Debug.Log("[FB] Not yet initialized. Will init again!");
            FB.Init(new InitDelegate(this.OnInitComplete), null, null);
            return;
        }
        new LoginWithReadPermissions(this.READ_PERMISSIONS, successAction, failureAction);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-02
    • 2013-11-17
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    相关资源
    最近更新 更多