vendredi 22 septembre 2023

Best practice/ideas to invoke a methods based on network text commands? [closed]

I have a project in C# which includes controlling an external device - for example a motorized stage. I have a class which communicates with the stage and expose an API to control it / report its status.

I wish to have a server program that controls said stage, and can get text requests (json) over network, and based on the request content, activates the relevant method on the device class with the supplied parameters.

The requests are all async (i.e. client doesn't block/wait for an answer) and as such the communication is done via async messaging (RabbitMQ) and not on some RPC framework (such as gRPC).

I have managed to do it so far with reflection, for example:

public void MesseageReciveCallback(string msg)
{
    RemoteCommand cmd = json.Deserialized(msg);
    Type deviceType = _motorizedStage.GetType();
    deviceType.InvokeMember(cmd.Name, 
                            BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, 
                            null, 
                            _motorizedStage, 
                            cmd.Parameters);
}

but I was wondering if there is a better way of doing this in an elegant way under above conditions, other than just having a big old switch block that go over the cmd.Name parameter and calling the correct method.

I would mention that performance is not an issue - that is, I know reflection is not ideal in term of of it, but we are talking about research setup used by few users, handling few request per seconds at most.

My main gripe is the current solution make the client send all the parameters in a object[], making this setup more prone to runtime errors - i.e. if I changed the parameters type/order in some function on the MotorizedStage class, the client code won't alert me to it automatically at compilation time.

Thanks in advance for any ideas.





Aucun commentaire:

Enregistrer un commentaire