我使用Nuget安装了Autofixture和Moq,所以我的Moq版本是4。

运行以下代码时

var fixture = new Fixture().Customize(new AutoMoqCustomization());
fixture.CreateAnonymous<ISomething>();


出现以下错误


System.IO.FileLoadException:可以
无法加载文件或程序集“ Moq”,
版本= 3.1.416.3,文化=中性,
PublicKeyToken = 69f491c39445e920'


我也尝试过将其重定向到v4,但是没有运气。

<configuration>
  <runtime>
    <assemblyBinding>
    <dependentAssembly>
      <assemblyIdentity name="Moq" publicKeyToken="69f491c39445e920" culture="neutral"/>
      <bindingRedirect oldVersion="3.1.416.3" newVersion="4.0.10827.0"/>
    </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>


这可能是什么问题?

最佳答案

为了重定向配置文件中的程序集绑定,您需要在<assemblyBinding> element中指定urn:schemas-microsoft-com:asm.v1命名空间,例如以下示例:

<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Moq"
                                  publicKeyToken="69f491c39445e920"
                                  culture="neutral"/>
                <bindingRedirect oldVersion="3.1.416.3"
                                 newVersion="4.0.10827.0"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>


有趣的是,由于Moq 3,使用.NET早期版本(例如AutoFixture 2.1In-Process Side-by-Side execution)编译的库程序集将自动加载到在.NET 4.0上运行的进程中。这是MSDN对此的报价:


如果使用
.NET Framework 4运行时,但是
包括一个内置的库
使用较早的运行时,该库
将使用.NET Framework 4运行时
也一样但是,如果您有
使用
较早的运行时和一个
使用.NET Framework 4构建的
必须强制您的申请也
使用.NET Framework 4


相关资源:


Redirecting Assembly Versions

关于.net - Autofixture和Moq v4,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6505591/

10-11 06:26
查看更多