0

可能有一种更简单的方法来做我正在做的事情,但我很好奇为什么我不能用两个函数调用这个构造函数。

我正在尝试编写某种排序器包装器来执行 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);
        }
    }
}
4

2 回答 2

8

您当前正在调用 Step1Go并且Step2Go- 而您希望使用方法组转换来创建Action委托:

StepMonitor stepMonitor = new StepMonitor(step1.Step1Go, step2.Step2Go);

此外,您的方法不接受任何参数,因此您需要更改StepMonitor为仅接受Action而不是Action<object>使用匿名函数来显式忽略参数:

StepMonitor stepMonitor = new StepMonitor(_ => step1.Step1Go(),
                                          _ => step2.Step2Go());

_只是一个被忽略的参数的有点传统的名称。您还可以使用:

StepMonitor stepMonitor = new StepMonitor(ignored => step1.Step1Go(),
                                          ignored => step2.Step2Go());

请注意,这些都与构造函数无关。如果你写的话,你现在会遇到同样的问题:

Action<Object> action = step1.Step1Go();

而我建议您应该有效地使用以下形式之一:

Action action = step1.Step1Go;
Action<object> action = ignored => step1.Step1Go();
于 2013-09-04T18:29:55.877 回答
1

你应该声明

StepMonitor stepMonitor = new StepMonitor(step1.Step1Go, step2.Step2Go);

但该方法的签名需要是:

public void Step1Go(object c)
        {
            Console.WriteLine("Enter String for Step1");
            string step1 = Console.ReadLine();
            TriggerStepCompleted();
        }

否则,如果您想让 Step1Go() 无参数,请将构造函数更改为Action而不是

 Action<Object>
于 2013-09-04T18:53:00.617 回答