是否可以抛出MessageQueueException?

时间:2008-10-24 23:10:46

标签: c# .net exception tdd rhino-mocks

我在RhinoMocks中使用模拟对象来表示调用MessageQueue.GetPublicQueues的类。我想模拟消息队列在工作组模式下运行时引发的异常,这是一个MessageQueueException,以确保我正确捕获异常

MessageQueueException没有公共构造函数,只有异常的标准受保护构造函数。是否有适当的方法从mock对象/ Expect.Call语句中抛出此异常?

2 个答案:

答案 0 :(得分:5)

反射可以破坏可访问性规则。您无效保修,.NET更新可能会轻易破坏您的代码。试试这个:

using System.Reflection;
using System.Messaging;
...
        Type t = typeof(MessageQueueException);
        ConstructorInfo ci = t.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, 
          null, new Type[] { typeof(int) }, null);
        MessageQueueException ex = (MessageQueueException)ci.Invoke(new object[] { 911 });
        throw ex;

答案 1 :(得分:3)

您可以通过尝试创建无效队列来解决此问题。可能更安全,然后被框架更改(通过使用私有/受保护的构造函数)保留:

MessageQueue mq = MessageQueue.Create("\\invalid");
相关问题