举个例子:使用委托时,是否有办法获取对象响应?
普通代码:
private void wcDownloadStringCompleted(
object sender, DownloadStringCompletedEventArgs e)
{
// The result is in e.Result
string fileContent = (string)e.Result;
}
public void GetFile(string fileUrl)
{
using (WebClient wc = new WebClient())
{
wc.DownloadStringCompleted +=
new DownloadStringCompletedEventHandler(wcDownloadStringCompleted);
wc.DownloadStringAsync(new Uri(fileUrl));
}
}
但是如果我们用一个匿名delegate
,如:
public void GetFile(string fileUrl)
{
using (WebClient wc = new WebClient())
{
wc.DownloadStringCompleted +=
delegate {
// How do I get the hold of e.Result here?
};
wc.DownloadStringAsync(new Uri(fileUrl));
}
}
如何获得持有e.Result
那里?