title: Moq: Unable to cast object of type Castle.Proxies.IMessageProxy' to type 'MyProject.Message'
body: <p>I have the following extension method to serialise an object into XML using the <code>XmlSerialiser</code> class:</p>
  
  <pre><code>public static string Serialise&lt;T&gt;(this T value)
  {
      if (value == null)
      {
          return string.Empty;
      }
  
      var xmlserializer = new XmlSerializer(typeof(T));
      var stringWriter = new StringWriter();
      using (var writer = XmlWriter.Create(stringWriter))
      {
          xmlserializer.Serialize(writer, value);
          return stringWriter.ToString();
      }
  }
  </code></pre>
  
  <p>Since <code>XmlSerialiser</code> cannot serialise an interface type, I am casting the interface type to concrete type. This is a .NET Standard 1.3 project.</p>
  
  <pre><code>public void Submit(IMessage message)
  {
      var xml = ((Message)message).Serialise();
  }
  </code></pre>
  
  <p>Using <code>MsTest</code> and <code>Moq 4.8.1</code>, the following test fails with this message: <code>Unable to cast object of type Castle.Proxies.IMessageProxy' to type 'MyProject.Message'</code></p>
  
  <pre><code>[TestMethod]
  public void Submit()
  {  
      var mockMessage = new Mock &lt;IHospitalSendBatchMessage&gt;();
      _service.Submit(mockMessage.Object);
  }
  </code></pre>
  
  <p>I understand that I should not have to cast an interface to a concrete type but this is the quickest way to get the serializer working. Is there any way to mock the casting operation or another approach to solving this problem?</p>