title: TypeLoadException when trying to mock IObjectSet with Moq
body: <p>I have the following setup code:</p>
  
  <pre><code>MockOf&lt;IObjectSet&lt;Dummy&gt;&gt;().Setup(c =&gt; c.AddObject(dummy)).Verifiable();
  MockOf&lt;IObjectContextWrapper&gt;().Setup(c =&gt; c.GetObjectSet&lt;Dummy&gt;()).Returns(MockOf&lt;IObjectSet&lt;Dummy&gt;&gt;().Object);
  </code></pre>
  
  <p>where <code>Dummy</code> is an empty class definition, and <code>dummy</code> is a <code>Dummy</code>. <code>MockOf&lt;T&gt;()</code> is a mock managing feature on a base class, which basically makes sure that each time it's called on a type, it returns the same mock instance.</p>
  
  <p>The test containing this setup code fails with a <code>TypeLoadException</code> and the following message:</p>
  
  <blockquote>
    <p>System.TypeLoadException : Type 'IObjectSet`1Proxy389e220f10aa4d9281d0b9e136edc1d4' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a621a9e7e5c32e69' is attempting to implement an inaccessible interface.</p>
    
    <p>at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)<br>
    at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()<br>
    at System.Reflection.Emit.TypeBuilder.CreateType()<br>
    at Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType()<br>
    at Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.GenerateCode(Type proxyTargetType, Type[] interfaces, ProxyGenerationOptions options)<br>
    at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, IInterceptor[] interceptors)<br>
    at Moq.Mock<code>1.&lt;InitializeInstance&gt;b__0()<br>
    at Moq.Mock</code>1.InitializeInstance()<br>
    at Moq.Mock`1.get_Object()<br>
    at OddEnds.Tests.Data.EntityFramework.RepositoryTest.Delete_DeletesObjectFromObjectSet() in RepositoryTest.cs: line 43 </p>
  </blockquote>
  
  <p>I have imported <code>System.Data.Objects</code> and referenced both System.Data.Entity.dll and Microsoft.Data.Entity.CTP.dll in both the test project and the project where the class being tested resides. The build succeeds with no errors, warnings or messages (except a few related to Code Contracts...)</p>
  
  <p><strong>How do I fix this?</strong></p>