首页 > 解决方案 > 是否可以使用字节 [] 将函数的多个参数值传递给另一个函数?

问题描述

需要一些帮助来编写位于“应用程序 A”内的函数,以使用字节 [] 将多个变量值传递到位于不同应用程序“应用程序 B”中的另一个函数。到目前为止,我能够发送一个变量值,但我真的很难传递多个变量值。在此先感谢您的帮助,节日快乐!

应用A代码:

public static void eventVideoData(string appID, int ID)
        {
            string application_ID = appID;
            int idP = ID;
            string pubHeader = "/SERVICES/REQUEST/ECG/UDCMGR/";

            //Client.Publish(topic, message, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);

            byte[] bytes = Encoding.ASCII.GetBytes(application_ID);

            progrm.ADCSOAPub.Publish(pubHeader + "VIDEODATA", bytes);

            Console.WriteLine("Message Sent");

        }

应用 B 代码:

if (e.Topic == "/SERVICES/REQUEST/ECG/UDCMGR/IMAGEDATA")
            {
                //Logic for message received?

                Console.WriteLine(msg1);

                string pubHeader = "/SERVICES/RESPONSE/ECG/UDCMGR/";

                Client.Publish(pubHeader + "IMAGEDATA", Encoding.ASCII.GetBytes("1"));
               
                Console.WriteLine("Message Sent to Scripting Engine");
            }

标签: c#

解决方案


您可以创建一个元组并作为字节数组发送,然后隐蔽回元组检索属性值,这可能并不优雅,但您可以实现您想要的。

private byte[] ApplicationA()
        {
            (string topic, string message) obj = (
                            topic: "topic",
                            message: "message"
                        );
           
            var bf = new BinaryFormatter();
            using MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, obj);
            return ms.ToArray();
        }

        private (string,string) ApplicationB(byte[] input)
        {
           
            var bf = new BinaryFormatter();
            using var ms = new MemoryStream(input);
            var  obj = bf.Deserialize(ms);
            return ((string, string))obj;
        }

        [Fact]

        public void Test()
        {
            var byteArray = ApplicationA();
            var t = ApplicationB(byteArray);
            Console.WriteLine(t.Item1);
            Console.WriteLine(t.Item2);
        }

推荐阅读