, -, . , , .
ret , . , , , , .
, GetWebStuff , , " " " [...]". , , , , , .
:
public Task<dynamic> GetWebStuff()
{
var tcs = new TaskCompletionSource<dynamic>();
WebClient wc = new WebClient();
wc.DownloadStringCompleted += async (s, a) =>
{
tcs.TrySetResult(await Newtonsoft.Json.JsonConvert.DeserializeObject(
a.Result.ToString()));
};
wc.DownloadStringAsync(new Uri("http://www.MyJson.com"));
return tcs.Task;
}
, , , .
, , , :
class ClosureClass
{
public TaskCompletionSource<dynamic> tcs;
public async Task AnonymousMethod1(object s,
DownloadDataCompletedEventArgs a)
{
tcs.TrySetResult(await Newtonsoft.Json.JsonConvert.DeserializeObject(
a.Result.ToString()));
}
}
public Task<dynamic> GetWebStuff()
{
ClosureClass closure = new ClosureClass();
closure.tcs = new TaskCompletionSource<dynamic>();
WebClient wc = new WebClient();
wc.DownloadStringCompleted += closure.AnonymousMethod1;
wc.DownloadStringAsync(new Uri("http://www.MyJson.com"));
return closure.tcs.Task;
}
When a variable is closed, a new closure class is created with an instance variable for each private variable. The lambda turns into an instance method that uses these instance fields. The method that the lambda had creates an instance of this new type, uses its fields, not local ones, wherever they are closed above the locales, and then uses the new named method where the lambda was.
Servy source
share