【发布时间】: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