0

私がやっていることをもっと簡単に行う方法があるかもしれませんが、なぜこのコンストラクターを 2 つの関数で呼び出せないのか不思議です。

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);

さらに、メソッドはパラメーターを取らないため、代わりに受け入れるように変更する、匿名関数を使用してパラメーターを明示的に無視する必要があります。StepMonitorActionAction<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 に答える