在下面的代码中,我定义了一个事件处理程序,并希望从中访问age和name变量,而无需全局声明名称和年龄。有什么办法可以说e.agee.name吗?

void Test(string name, string age)
{
    Process myProcess = new Process();
    myProcess.Exited += new EventHandler(myProcess_Exited);
}

private void myProcess_Exited(object sender, System.EventArgs e)
{
  //  I want to access username and age here. ////////////////
    eventHandled = true;
    Console.WriteLine("Process exited");
}

最佳答案

是的,您可以将事件处理程序定义为lambda表达式:

void Test(string name, string age)
{
  Process myProcess = new Process();
  myProcess.Exited += (sender, eventArgs) =>
    {
      // name and age are accessible here!!
      eventHandled = true;
      Console.WriteLine("Process exited");
    }

}

关于c# - 将参数传递给事件处理程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12293471/

10-16 10:51