I’m developing a scripting engine for an embedded system automation project. One requirement to accomplish this is to store instructions sequentially to be run later. The project also has to be in C#.
The “instructions” are any method that the user wishes to run, with the required parameter. They don’t have a specific pattern. Most of the methods are async and run with an await, since they are related to the response of the hardware. After creating the list of methods/arguments, running instructions.Execute()
would run the methods sequentially.
Probably because I’m new to C#, I can’t seem to find a way to accomplish this achieving the necessary flexibility.
Summarizing, I’ll have an Instruction class where each instance will store a method and it’s parameters.
The method can be any method (returns void, Task, object, any type) and can have any parameter (void, single parameter, multiple parameter, preferably being able to externalize variables with things like pointers).
I’ve made a proof of concept for the entire system and this part was solved with generic types. The issue is that this solution will only work for single parameter methods.
public class Instruction<T, U>
{
/// The method to be executed. The method must have one argument.
private readonly Func<T, U>? _Function;
/// The method to be executed. The method must have one argument.
private readonly Action<T>? _Action;
/// The argument going to be passed to the method
private readonly T _Argument; // TODO: This could be a list of arguments for more than 1 argument methods
public Instruction(Func<T, U> function, T argument)
{
_Function = function;
_Argument = argument;
_Action = null;
}
public Instruction(Action<T> function, T argument)
{
_Function = null;
_Argument = argument;
_Action = function;
}
/// Executes the function(argument) passed in the constructor.
public async Task<bool> Execute()
{
if (_Action != null)
{
_Action(_Argument);
return true;
}
else if (_Function == null) // _Action and _Function is null.
{
return true;
}
// Invoke the function with the argument
var result = _Function(_Argument);
// Check if the result is a Task
if (result is Task task)
{
// If it's a Task, await it
await task;
}
else if (result is Task<U> taskWithResult)
{
// If it's a Task<U>, await it and ignore the result
await taskWithResult;
}
else
{
// If it's neither, it's a synchronous operation, so no awaiting is necessary
// This branch is executed for synchronous methods like Console.WriteLine
}
return true;
}
}
I’ve also tried to solve it with delegates, but no luck.
Bellow is a silly example on how my implementation is used:
Test test = new Test();
test.AddInstruction(Console.WriteLine, "Silly Example");
for (var index = 0; i < 10; i++) {
test.AddInstruction(HMI.ButtonPress, index);
test.AddInstruction(Task.Delay, 250);
}
test.AddInstruction(ECU.TurnOnBlinker, LIGHTS.LeftBlinker);
test.AddInstruction(Task.Delay, 1000);
test.AddInstruction(ECU.TurnOffBlinker, LIGHTS.LeftBlinker);
test.AddInstruction(Console.WriteLine, "Executed all instructions");
test.Execute();
But now I need more flexibility. I’m at a loss for some work days now, can anyone give me a north?