C#将参数传递给委托

时间:2017-11-27 12:56:47

标签: c#

您好我的C#语法有点问题......

现在我将四个参数传递给一个名为WriteConfig

的方法
 var task = fac.StartNew(() => { vault.WriteConfig(fileName, id, queue, DeleteUploadQueue); });

DeleteUploadQueue是一个委托,并在id内以WriteConfig作为参数进行调用。我想要做的是将id传递到DeleteUploadQueue外面而不是WriteConfig内,这样我就不必将id传递给WriteConfig

WriteConfig看起来像这样:

 public void WriteConfig(string fileName, int id, BlockingCollection<byte[]> queue, Action<int> deleteFunc)

但我无法想出正确的语法。

感谢您的帮助

2 个答案:

答案 0 :(得分:3)

假设您将签名更改为:

public void WriteConfig(string fileName, BlockingCollection<byte[]> queue, Action deleteFunc)

你可以写

var task = fac.StartNew(() => { vault.WriteConfig(fileName, queue,  ()=> DeleteUploadQueue(id)); });

答案 1 :(得分:2)

听起来像你想要的

var task = fac.StartNew(() => { vault.WriteConfig(fileName, id, queue,
    () => DeleteUploadQueue(id)); })

因此,如果您 想要将WriteConfig更改为不int id,那么您需要将deleteFunc变为Action而不是Action<int>,所以:

public void WriteConfig(string fileName, BlockingCollection<byte[]> queue,
    Action deleteFunc)

var task = fac.StartNew(() => { vault.WriteConfig(fileName, queue,
    () => DeleteUploadQueue(id)); })