I get the Oops! code message when submitting the question below, any idea why?

Oops! Your question couldn't be submitted because:

Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.

Moq throws a TypeInitializationException when using Moles with the [HostType("Moles")]. Code example and exception below. 

    public void TestMethod()
        var mock = new Mock<IAnything>();

The exception:

System.TypeInitializationException occurred
  Message=The type initializer for 'Moq.Extensions' threw an exception.
       at Moq.Extensions.IsMockeable(Type typeToMock)
       at Moq.Mock`1.CheckParameters()
       at Moq.Mock`1..ctor(MockBehavior behavior, Object[] args)
       at Moq.Mock`1..ctor(MockBehavior behavior)
       at Moq.Mock`1..ctor()
       at BP.DFR.Common.Services.Network.DfrNetworkQueryTests.MyTestMethod()
  InnerException: System.Security.VerificationException
       Message=Operation could destabilize the runtime.
            at Moq.Extensions..cctor()

The link below suggested it might be a moq version issue, but it happens in both version 3.1 and 4.0 for me.


I'm using VS 2010 Premium SP1. Anybody had any luck resolving this?

marked as duplicate by Shadow The Dragon Wizard support Jun 8 '18 at 21:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I've also tried escaping the [HostType("Moles")] with backticks, but no joy. – SDK Aug 13 '12 at 8:15

Looking at your code, you probably haven't indented below line properly:

System.TypeInitializationException occurred

You need to indent your code by four spaces, or select it all and press the {} button. Also make sure there is one blank line between your last line of text and your first line of code. This will cause your code to be rendered in <pre><code> blocks and all of its HTML/Markdown entities will be escape/ignored respectively.

Properly indented code renders like this:

#include <iostrea>

int main() {
  vector<int> items;

Unindented code renders like this:


int main() { vector items; }

You can see why we stress proper indenting of code.

  • No, the part of the code beginning with System.TypeInitializationException occurred is within a <pre> block, that works too. – Gilles Aug 10 '12 at 16:32
  • Yip the System.TypeInitializationException is not code, just an exception message, that's why I put in the <pre> block. I don't know if it has something to do with the [HostType("Moles")] in the first line being seen as code. But I don't really want that line as code. Thanks for the reply, still no joy though :( – SDK Aug 11 '12 at 10:11
  • I didn't actually try your suggestion, thought Gilles was correct with his comment. But you're right the exception message needs to be indented. I wouldn't have considered it code, but hey. Thanks. – SDK Aug 13 '12 at 8:23

Not the answer you're looking for? Browse other questions tagged .