可能有一种更简单的方法来做我正在做的事情,但我很好奇为什么我不能用两个函数调用这个构造函数。
我正在尝试编写某种排序器包装器来执行 Step1、启动 Step2、启动 Step3 等。
当我调用 StepMonitor 的构造函数时程序失败 我
得到错误:无法将类型“void”隐式转换为“System.Action”
static void Main(string[] args)
{
Step1 step1 = new Step1();
Step2 step2 = new Step2();
StepMonitor stepMonitor = new StepMonitor(step1.Step1Go(), step2.Step2Go()); // Fails here
}
public class StepMonitor
{
#region Function To Monitor
Action<object> _objectToMonitor;
#endregion
#region Function to Execute on Event
Action<object> _objectToExecute;
#endregion
#region Constructor
public StepMonitor(Action<object> objectToMonitor, Action<object> objectToExecute)
{
_objectToMonitor = objectToMonitor;
_objectToExecute = _objectToExecute;
_objectToMonitor += _objectToExecute;
}
#endregion
}
public class Step1
{
public event EventHandler StepCompletedHandler; // the Event
public Step1()
{
}
public void Step1Go()
{
Console.WriteLine("Enter String for Step1");
string step1 = Console.ReadLine();
TriggerStepCompleted();
}
protected virtual void TriggerStepCompleted() // the Trigger. Foo calls this to raise the event
{
// make a copy to be more thread-safe
EventHandler handler = StepCompletedHandler;
if (handler != null)
{
// invoke the subscribed event-handler(s)
handler(this, null);
}
}
}
public class Step2
{
public event EventHandler StepCompletedHandler; // the Event
public Step2()
{
}
public void Step2Go()
{
Console.WriteLine("Enter String for Step2");
string step2 = Console.ReadLine();
TriggerStepCompleted();
}
protected virtual void TriggerStepCompleted() // the Trigger. Foo calls this to raise the event
{
// make a copy to be more thread-safe
EventHandler handler = StepCompletedHandler;
if (handler != null)
{
// invoke the subscribed event-handler(s)
handler(this, null);
}
}
}