简体   繁体   English

如何解释这个 C# lambda 示例

[英]How to intrepret this C# lambda example

I am really struggling to understand what is going on with this code, what does the single line lambda return if anything?我真的很难理解这段代码是怎么回事,如果有的话,单行 lambda 会返回什么? I am unclear on what that 3rd argument is going to be?我不清楚第三个参数是什么?

   public Task<StatusMessage> MarketSubscription(MarketSubscriptionMessage message)
    {
        int id = NextId();
        message.Id = id;
        message.Op = REQUEST_MARKET_SUBSCRIPTION;
        var newSub = new SubscriptionHandler<MarketSubscriptionMessage, ChangeMessage<MarketChange>, MarketChange>(id, message, false);

        return SendMessage(new RequestResponse(id, message, 
                            success => MarketSubscriptionHandler = newSub));
    }

For info the other method is有关信息,另一种方法是

public RequestResponse(int id, RequestMessage request, Action<RequestResponse> onSuccess)
{
    Id = id;
    Request = request;
    OnSuccess = onSuccess;
}

I would be most grateful if anyone could explain how to intrepret that last complex line.如果有人能解释如何解释最后一行复杂的代码,我将不胜感激。 Thanks.谢谢。

Well RequestResponse:那么请求响应:

public RequestResponse(int id, RequestMessage request, Act ion<RequestResponse> onSuccess)
{
    Id = id;
    Request = request;
    OnSuccess = onSuccess;
}

takes 3 argumenta, and ID, a message and an Action to call, presumably when some reply is received.需要 3 个参数和 ID,一条消息和一个要调用的操作,大概是在收到回复时。 The Action takes an argument that contains the response, an instance of RequestResponse. Action 采用包含响应的参数,即 RequestResponse 的实例。

The calling code sets up this callback (ACtion) here调用代码在此处设置此回调 (ACtion)

new RequestResponse(id, message, 
                        success => MarketSubscriptionHandler = newSub)

the last argument is the Action.最后一个参数是动作。 It is a bit odd in that it doesnt do anything with the response, nor does it run any code.有点奇怪的是它不对响应做任何事情,也不运行任何代码。 It simply sets MarketSubscriptionHandler (presumably a class member variable) to newSub .它只是将MarketSubscriptionHandler (大概是一个 class 成员变量)设置为newSub newSub is新订阅是

    var newSub = new SubscriptionHandler<MarketSubscriptionMessage, ChangeMessage<MarketChange>, MarketChange>(id, message, false);

My guess is that after the code you show exits another piece of code does something with MarketSubscriptionHandler我的猜测是,在您显示的代码退出后,另一段代码对 MarketSubscriptionHandler 做了一些事情

In your example code, the 3rd argument to SendMessage() is an action named onSuccess , which appears to be a delegate that is invoked at a later time when/if the request has succeeded.在您的示例代码中, SendMessage()的第三个参数是一个名为onSuccess的操作,它似乎是一个委托,在请求成功时/如果请求成功,稍后将调用该委托。

This usage of an action allows the caller of SendMessage() to specify any action they like which receives RequestResponse as an argument.动作的这种用法允许SendMessage()的调用者指定他们喜欢的接收RequestResponse作为参数的任何动作。 A caller could specify an action to log something, execute another method, etc, or in this case, assign newSub as the MarketSubscriptionHandler .调用者可以指定一个操作来记录某些内容、执行另一个方法等,或者在这种情况下,将newSub指定为MarketSubscriptionHandler

Basically what's happening is:基本上发生的事情是:

  1. The message is sent via SendMessage()消息通过SendMessage()发送
  2. When response is received, SendMessage() checks to see if onSuccess should be invoked.收到响应后, SendMessage()检查是否应调用onSuccess
  3. If response is deemed successful, onSuccess is invoked (which in this case assigns newSub as the MarketSubscriptionHandler ).如果响应被视为成功,则调用onSuccess (在这种情况下将newSub指定为MarketSubscriptionHandler )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM