Action delegate in .NET2 - Using the generic type

2019-02-22 21:16发布

问题:

I'm porting working code from .NET4 to .NET2 (a WinCE device).

The usage of Action taking no arguments and returning no value isn't allowed in .NET2

compile error on line 5 below: Using the generic type 'System.Action' requires '1' type arguments

Workaround thoughts?

//first state is the default for the system
    public enum States { EnterVoucherCode, EnterTotalSale, ProcessVoucher };
    public enum Events { PressNext, PressRedeem, ProcessSuccess, ProcessFail, PressBackToVoucherCode };

    public States State { get; set; }

    private Action[,] fsm; //Fails to compile here

    public FiniteStateMachine()
    {
        //array of action delegates
        fsm = new Action[3, 5] { 
        //PressNext,     PressRedeem,            ProcessSuccess,      ProcessFail,      PressBackToVoucherCode

回答1:

Indeed, the non-generic Action was added in .NET 3.5.

However, Action is just an ordinary delegate type, so you could simply roll your own, like so:

public delegate void Action();


标签: c# delegates