再写mock对象2
上文描述的是读写系统的内置类型,如果是我们自定义的类型又怎么样呢,于是进行了一次测试,添加一个动态链接库项目,并引用到进行读写的两个项目里,下面是测试代码
自定义类型
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ElementX { public class DataX { public string s; } }
写入项目
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Messaging; using ElementX; namespace PushMessage { class Program { static void Main(string[] args) { MessageQueue queue = null; string queueName = ".\\Private$\\TEST"; if (MessageQueue.Exists(queueName)) { queue = new MessageQueue(queueName); } else { queue = MessageQueue.Create(queueName, false); queue.SetPermissions("Everyone", MessageQueueAccessRights.FullControl); } for (int i = 0; i < 3;i++ ) { DataX dx = new DataX(); dx.s = "dxdx"; queue.Send(dx); Console.WriteLine("push DX"); } } } }
读取模块
using System; using System.Collections.Generic; using System.Linq; using System.Text; using QueueSpace; using ElementX; namespace QueueTest { class Program { static void Main(string[] args) { Queue queue = QueueFactory.makeQueue(); while (true) { object msg = queue.readMessage(typeof(DataX)); Console.WriteLine(((DataX)msg).s); } } } }