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

.NET 8 WinForms AutoCAD插件中加载System.Runtime.Serialization.Formatters程序集失败求助

.NET 8 WinForms AutoCAD插件中加载System.Runtime.Serialization.Formatters程序集失败求助

大家好,我在开发基于.NET 8的WinForms AutoCAD插件时遇到了一个程序集加载失败的问题,折腾了好一阵都没解决,想请教下各位的思路。

问题现象

当调用自定义的序列化方法时,立刻触发了System.IO.FileLoadException错误,具体错误内容如下:

System.IO.FileLoadException: 'Could not load file or assembly 'System.Runtime.Serialization.Formatters, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Could not find or load a specific file. (0x80131621)'

已尝试的解决操作

我已经按照官方文档和常规排查步骤做了以下尝试,但问题依然存在:

  • 在项目文件中添加了启用不安全BinaryFormatter序列化的配置:
<PropertyGroup>
  ...
  <EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>
  • 在代码中通过#pragma指令禁用了SYSLIB0011警告,当前代码里已经看不到相关警告
  • 通过NuGet引入了对应版本的程序集依赖:
<ItemGroup>
  ...
  <PackageReference Include="System.Runtime.Serialization.Formatters" Version="9.0.7" />
</ItemGroup>
  • 手动在AutoCAD中加载了System.Runtime.Serialization.Formatters.dll,但没有效果
  • 检查了输出目录,确认该DLL文件确实存在

触发错误的代码

错误会在调用下面这个序列化方法时立即触发:

private static byte[] SerializeWithBinaryFormatter(List<double[]> list)
{
    // .NET Core迁移后添加的警告抑制
    #pragma warning disable SYSLIB0011
    BinaryFormatter bf = new BinaryFormatter();
    #pragma warning restore SYSLIB0011

    byte[] byteStream = null;
    using (MemoryStream ms = new MemoryStream())
    {
        bf.Serialize(ms, list);
        byteStream = ms.ToArray();
    }
    return byteStream;
}

这个代码是AutoCAD插件的一部分,我实在搞不懂为什么明明依赖加了、DLL也在输出目录里,还是会出现加载失败的情况,希望有经验的朋友能给我一些建议或者解决方案。


内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 11:49:33