【问题标题】:Autocomplete lambda expression (when it used like functions parameter) (C#, Visual Studio 2019)自动完成 lambda 表达式(当它使用类似函数参数时)(C#,Visual Studio 2019)
【发布时间】:2020-01-14 02:48:56
【问题描述】:

我无法让开发工作室用正确的数据替换 lambda 表达式作为被调用函数的参数。如何做到这一点?

我创建了一个处理网络请求的类。在这个类中声明了三个委托,并且有一个向服务器发出请求的函数。这个函数的参数是:一个给服务器的命令和三个回调函数(委托)。我在键盘上按下了不同的组合,但它们都不允许自动替换 lambda 函数,我必须不断用手填充所有内容。

http 管理器:

namespace ProglotClientAdminPanel.managers
{
    public delegate void RequestCompletedCallBack(SimpleAnswer answer);
    public delegate void RequestSuccessCallback(SimpleAnswer answer);
    public delegate void RequestErrorCallback(SimpleAnswer answer, string errorMsg);


    public class ApiRequestHelperAsync
    {
        /// <summary>
        /// send request
        /// </summary>
        /// <param name="simpleCommandIn">SimpleCommand - command model</param>
        /// <param name="callBackIn">global callback</param>
        /// <param name="successCallbackIn">success callback</param>
        /// <param name="errorCallbackIn">error callback</param>
        public void sendRequest(SimpleCommand simpleCommandIn, 
            RequestCompletedCallBack callBackIn = null,
            RequestSuccessCallback successCallbackIn = null, RequestErrorCallback errorCallbackIn = null)
        {
            simpleCommand = simpleCommandIn;
            callBack = callBackIn;
            successCallback = successCallbackIn;
            errorCallback = errorCallbackIn;
            sendRequestToServer();//send http-request to server
        }
    }
}

这就是我使用 lambda-callback 发出 http-request 的方式(它有效):

SimpleCommand command = new SimpleCommand("saveStock");
command.addParam("org_id", user.org_id);
command.addParam("user_id", user.user_id);
command.addParam("stock", editableStock);

new ApiRequestHelperAsync().sendRequest(command,
    null,
    (SimpleAnswer answer) =>
    {

    },
    (SimpleAnswer answer, string errorMsg) =>
    {

    }
    );

如何强制 Visual Studio(2017 或 2019)使用键盘组合粘贴函数 lambda-param?

    (SimpleAnswer answer) =>
    {

    },
    (SimpleAnswer answer, string errorMsg) =>
    {

    }

如您所见,代码 sn-p 不是很好的解决方案,因为函数或 http-class 可能在任何项目中更改。

【问题讨论】:

    标签: c# lambda delegates visual-studio-2019


    【解决方案1】:

    所以,我只看到一个解决方案:始终为实际版本的类更新自己的代码 sn-ps。我对这个问题的变体:

    <?xml version="1.0" encoding="utf-8" ?>
    <CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
        <CodeSnippet Format="1.0.0">
            <Header>
                <Title>Simple API http-request (2 callbacks)</Title>
                <Shortcut>httpLambded_2callbacks</Shortcut>
                <Description>Make simple http-request with 2 lambda-callbacks</Description>
                <Author>Lunev N.F.</Author>
                <SnippetTypes>
                    <SnippetType>Expansion</SnippetType>
                    <SnippetType>SurroundsWith</SnippetType>
                </SnippetTypes>
            </Header>
            <Snippet>
                <Code Language="csharp">
                <![CDATA[
                new ApiRequestHelperAsync().sendRequest(command,
                    null,
                    (SimpleAnswer answer) =>
                    {
                        //on success
    
                    },
                    (SimpleAnswer answer, string errorMsg) =>
                    {
                        //on error
    
                    }
                    );      
                ]]>
                </Code>
            </Snippet>
        </CodeSnippet>
    </CodeSnippets>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 2012-02-18
      • 2012-01-03
      • 2019-03-13
      相关资源
      最近更新 更多