我有一个班级Account
。在那里,我添加一个名为Activation
的动作,并为其附加一个事件处理程序。
这是代码:
public class Account
{
public event Action Activation;
public Account()
{
this.Activation += SendActivationEvent;
}
private void SendActivationEvent()
{
//do something here
}
}
这很好用。现在我想在SendActivationEvent
方法中添加一个参数。我试过这样的:
string sourceID = String.Empty;
this.Activation += SendActivationEvent(sourceID);
private void SendActivationEvent(string sourceID)
{
//do something here
}
它在SendActivationEvent(sourceID)
无法将Void类型隐式转换为System.Action
我该怎么做才能解决这个问题?
答案 0 :(得分:2)
好吧,Action
不接受参数。请改为使用Action<T>
(在您的情况下为Action<String>
):
public class Account
{
// Note "String"
public event Action<String> Activation;
public Account()
{
// Assign method i.e. "SendActivationEvent",
// not method's result SendActivationEvent(sourceID)
this.Activation += SendActivationEvent;
}
private void SendActivationEvent(string sourceID)
{
//TODO: do something here
}
}