How to invoke methods with arguments for any return and argument type?

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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật