You can do it using the Action delegate
List<Action> Undo = new List<Action>();
Undo.Add(() => SetText(1, "Hello"));
Undo.Add(() => SetText(2, "World"));
foreach (var u in Undo)
{
u();
}
Or you can use interfaces to create classes that are suited to different types of undo action. This method is more flexible as you can have different classes responsible for different types of action with their own parameters etc
public interface IUndo
{
void Undo();
}
public class SetTextUndo : IUndo
{
public int Index { get; set; }
public string Text { get; set; }
public void Undo()
{
Console.WriteLine("Undo for {0} {1}", this.Index, this.Text);
}
}
public class FormattingUndo : IUndo
{
public enum FormatTypeEnum
{
Bold,
Italic,
Underline
}
public FormatTypeEnum FormatType { get; set; }
public void Undo()
{
Console.WriteLine("Undo formatting {0}", this.FormatType);
}
}
List<IUndo> Undo = new List<IUndo>();
Undo.Add(new SetTextUndo { Index = 1, Text = "Hello" });
Undo.Add(new SetTextUndo { Index = 2, Text = "World" });
Undo.Add(new FormattingUndo { FormatType = FormattingUndo.FormatTypeEnum.Italic });
foreach (var u in Undo)
{
u.Undo();
}