不能将方法作为委托传递

时间:2015-03-23 15:14:06

标签: c# delegates

我想通过这个方法

private void activityStreamDownloaded(TaskNotification tn)
{
    //Logic
}

使用Delegate ad = activityStreamDownloaded;方法

public void AddNextTask(WebAPITask task, Delegate callback)
{
    //Error: Callback is variable but is used like a method
    task.OnExecuted += () => callback(task.TaskNotification);
}

喜欢这个_taskList.AddNextTask (new TaskObj(), ad);

为什么会发生这种情况,我该如何解决?

1 个答案:

答案 0 :(得分:0)

正如评论中所指出的那样Delegate是一个抽象类。而是尝试这样的事情;

// declare an action type (basically a delegate with void return type)
Action<TaskNotification> callback = activityStreamDownloaded;


// update method signature to take an Action<TaskNotification>
public void AddNextTask(WebAPITask task, Action<TaskNotification> callback)

// this code should work now
_taskList.AddNextTask (new TaskObj(), callback);