Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何访问事件中的其他类_C#_.net - Fatal编程技术网

C# 如何访问事件中的其他类

C# 如何访问事件中的其他类,c#,.net,C#,.net,我试图在文件下载完成时触发的事件中访问下面代码块中的脚本类。我怎么能做到呢 public void DownloadScript(Script script, string DownloadLocation) { AddLog(GenerateLog("Downloading Script", "Started", "Downloading " + script.Name + " from " + script.DownloadURL + "."));

我试图在文件下载完成时触发的事件中访问下面代码块中的脚本类。我怎么能做到呢

 public void DownloadScript(Script script, string DownloadLocation)
    {


        AddLog(GenerateLog("Downloading Script", "Started", "Downloading " + script.Name + " from " + script.DownloadURL + "."));
        WebClient Client = new WebClient();
        Client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(Client_DownloadFileCompleted);
        Client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Client_DownloadProgressChanged);
        Client.DownloadFileAsync(new Uri(script.DownloadURL), DownloadLocation + "test1.zip");

    }
下面是触发的事件

public void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {

        if (e.Error.Message != string.Empty)
        {
            AddLog(GenerateLog("Downloading Script", "Error", "There was an error downloading " + script.Name + " from " + script.DownloadURL + ". " + e.Error.Message));
            Console.WriteLine("Error");

        }
        else
        {
            AddLog(GenerateLog("Downloading Script", "Done", "Finished downloading " + script.Name + " from " + script.DownloadURL + "."));
            Console.WriteLine("Done");
        }

    }

您可以使用lambda表达式捕获
脚本
对象,并将其作为额外参数传递给处理程序

public void DownloadScript(Script script, string DownloadLocation) {
  ...
  WebClient Client = new WebClient();
  Client.DownloadFileCompleted += (sender, e) => Client_DownloadFileCompleted(
    sender, 
    e, 
    script);
}

public void Client_DownloadFileCompleted(
  object sender, 
  AsyncCompletedEventArgs e,
  Script script) {
  ...
}

谢谢这正是我所需要的,以使这个工作。我们一直在到处寻找答案。