You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Moq依赖升级后调用AsSpan()抛FileLoadException,该如何排查?

问题:System.Runtime.CompilerServices.Unsafe版本不匹配引发FileLoadException

依赖层级

Moq 4.18.4的依赖链如下:

Moq 4.18.4
  System.Threading.Tasks.Extensions >= 4.5.4
    System.Runtime.CompilerServices.Unsafe >= 4.5.3

强制使用的版本

我的应用必须使用:

System.Runtime.CompilerServices.Unsafe 4.7.0

从依赖定义来看,4.7.0满足>=4.5.3的要求,但执行调用System.Memory中AsSpan()的单元测试时,Debug和Release模式都会抛出以下异常:

异常信息

System.IO.FileLoadException
HResult=0x80131040
Message=Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, 
Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its 
dependencies. The located assembly's manifest definition does not match the assembly 
reference. (Exception from HRESULT: 0x80131040)
Source=System.Memory
StackTrace:
   at System.MemoryExtensions.AsSpan(String text)
   at UnitTestProject1.UnitTest1.TestMethod1() in 
C:\source\repos\ConsoleApp9\UnitTestProject1\UnitTest1.cs:line 13

重现步骤

  1. 创建包含以下NuGet包的单元测试项目:
    • Moq Version="4.20.69"
    • MSTest.TestAdapter Version="2.2.8"
    • MSTest.TestFramework Version="2.2.8"
    • System.Memory Version="4.5.4"
    • System.Runtime.CompilerServices.Unsafe Version="4.7.0"
  2. 运行测试,会在b = a.AsSpan()行抛出上述异常。

测试代码

namespace UnitTestProject1
{
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            string a = "asdfdsa";
            var b = a.AsSpan();
        }
    }
}

请问这是Bug还是我遗漏了必要配置?


内容的提问来源于stack exchange,提问作者NirMH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 00:22:47