c# - 是否可以使用字节 [] 将函数的多个参数值传递给另一个函数?
问题描述
需要一些帮助来编写位于“应用程序 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");
}
解决方案
您可以创建一个元组并作为字节数组发送,然后隐蔽回元组检索属性值,这可能并不优雅,但您可以实现您想要的。
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);
}
推荐阅读
- artificial-intelligence - 如何从 COCO 数据集中过滤类?
- java - Firebase 推送通知云消息传递如何访问附加数据作为通知
- octobercms - 如何配置 octobercms 插件设置?
- java - 在 JsonArray 中插入新元素
- javascript - 将表单数据发送到 firebase 函数
- javascript - 用户滚动时将 div 粘贴在屏幕上
- r - 根据不同行中的值替换某些行中的值
- android - 为 APK 触发时,“keytool”命令中缺少 MD5
- visual-studio - Visual Studio 2013 更新 5:无法创建无人参与安装
- mysql - 平日最近 3 天记录