减少本地定义字段的数量
我有一个类,它有许多遵循以下模式的方法:
public void GetSomeData1(DataParameters[] parameters, Action responseHandler)
{
_getSomeData1ResponseHandler = responseHandler;
Transactions.GetSomeData1(SomeData1ResponseHandler, parameters);
}
private void SomeData1ResponseHandler(Response response)
{
if (response != null)
{
CreateSomeData1(response.Data);
}
}
public void CreateSomeData1(Data data)
{
//Create the objects and then invoke the Action
...
if(_getSomeData1ResponseHandler != null)
{
_getSomeData1ResponseHandler();
}
}
因此,某些外部类调用 GetSomeData1 并传入一个 Action,该 Action 应该在数据可用时调用。 GetSomeData1 存储响应 Action 并调用 Transactions.GetSomeData1 (异步调用)。一旦异步调用完成,就会创建数据并调用原始传入的 Action。
问题是,对于每个不同的 GetSomeData 调用,我需要存储传入的 Action,以便稍后引用它。有没有办法可以将原始操作传递给最终方法(CreateSomeData1)而不存储它?
谢谢。
I have a class that has a number of methods that follow the below pattern:
public void GetSomeData1(DataParameters[] parameters, Action responseHandler)
{
_getSomeData1ResponseHandler = responseHandler;
Transactions.GetSomeData1(SomeData1ResponseHandler, parameters);
}
private void SomeData1ResponseHandler(Response response)
{
if (response != null)
{
CreateSomeData1(response.Data);
}
}
public void CreateSomeData1(Data data)
{
//Create the objects and then invoke the Action
...
if(_getSomeData1ResponseHandler != null)
{
_getSomeData1ResponseHandler();
}
}
So, some external class calls GetSomeData1 and passes in an Action, which should be called when the data is available. GetSomeData1 stores the response Action and calls Transactions.GetSomeData1 (an async call). Once that async call is finished, the data is created and the original passed in Action is called.
The thing is, for every different GetSomeData call I have, I need to store the passed in Action so I can reference it later. Is there a way I can somehow pass the original Action to the final method (CreateSomeData1) without storing it?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 lambda 表达式将数据作为捕获的变量传递。不需要类字段。
在内部,编译器将创建类和字段来保存数据,但您编写的代码更干净且更易于维护。
You can pass the data along as a captured variable with lambda expressions. No need for class fields.
Internally, the compiler will create classes and fields to hold the data, but the code you write is cleaner and easier to maintain.