Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否可以引发MessageQueueException?_C#_.net_Exception_Tdd_Rhino Mocks - Fatal编程技术网

C# 是否可以引发MessageQueueException?

C# 是否可以引发MessageQueueException?,c#,.net,exception,tdd,rhino-mocks,C#,.net,Exception,Tdd,Rhino Mocks,我在Rhinomock中使用一个mock对象来表示调用MessageQueue.GetPublicQueues的类。我想模拟消息队列在工作组模式下运行时引发的异常,这是一个MessageQueueException,以确保正确捕获异常 MessageQueueException没有公共构造函数,只有异常的标准受保护构造函数。有没有合适的方法从mock对象/Expect.Call语句引发此异常?反射会破坏可访问性规则z。您将使保修失效,.NET更新很容易破坏您的代码。试试这个: using Sys

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


MessageQueueException没有公共构造函数,只有异常的标准受保护构造函数。有没有合适的方法从mock对象/Expect.Call语句引发此异常?

反射会破坏可访问性规则z。您将使保修失效,.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;

您可以通过尝试创建无效队列来导致此问题。可能比被框架更改束缚更安全(通过使用私有/受保护的构造函数):


现在请记住,您这样做是因为每次他们修补或更新框架时,它都可能是一个突破性的更改。。。以前在prod系统中有过。真糟糕。为什么需要抛出此异常?你能不能也用MessageQueue做一些无效的事情来获取它?!
MessageQueue mq = MessageQueue.Create("\\invalid");