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

Linux下运行.NET DLL触发TypeLoadException问题求助

解决.NET DLL移植Linux后pythonnet加载的TypeLoadException问题

问题描述

将.NET DLL移植到Linux环境(Windows WSL2 Ubuntu 20.04),已安装.NET SDK并构建目标框架为net6.0的DLL。通过pythonnet的clr模块导入该DLL运行Python脚本时,触发以下TypeLoadException错误:

Exception has occurred: TypeLoadException
Could not load type of field 'MyWorkSpace.MyClass:_updateWatchdog' (37) due to: Could not resolve type with token 0100004a from typeref (expected class 'System.Timers.Timer' in assembly 'System.ComponentModel.TypeConverter, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') assembly:System.ComponentModel.TypeConverter, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a type:System.Timers.Timer member:(null)  at (wrapper managed-to-native)
System.RuntimeType.GetFields_native(System.RuntimeType,intptr,System.Reflection.BindingFlags,System.RuntimeType/MemberListType)
at System.RuntimeType.GetFields_internal (System.String name, System.Reflection.BindingFlags bindingAttr, System.RuntimeType+MemberListType listType, System.RuntimeType reflectedType) [0x0001b] in <de882a77e7c14f8ba5d298093dde82b2>:0 
at System.RuntimeType.GetField (System.String name, System.Reflection.BindingFlags bindingAttr) [0x00015] in <de882a77e7c14f8ba5d298093dde82b2>:0
at System.Type.GetField (System.String name) [0x00000] in <de882a77e7c14f8ba5d298093dde82b2>:0
at Python.Runtime.ClassManager.CreateClass (System.Type type) [0x00079] in <3972e4befd8f45a1b7c0e96a8f1f659a>:0
at Python.Runtime.ClassManager.GetClass (System.Type type) [0x00015] in <3972e4befd8f45a1b7c0e96a8f1f659a>:0
at Python.Runtime.ModuleObject.GetAttribute (System.String name, System.Boolean guess) [0x0008e] in <3972e4befd8f45a1b7c0e96a8f1f659a>:0
at Python.Runtime.ModuleObject.tp_getattro (System.IntPtr ob, System.IntPtr key) [0x00073] in <3972e4befd8f45a1b7c0e96a8f1f659a>:0 

尝试切换至.NET 3.1重新构建后,问题依旧。

测试脚本:

import clr
import os

dll_name = os.path.join(os.path.abspath(os.path.dirname(__file__)), ("mydll_l") + ".dll")
clr.AddReference(dll_name)
from MyNameSpace import MyClass
_MyObj = MyClass()
temp = _MyObj.GetTemperature()
print(temp)

WSL下的项目文件.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="somedll">
      <HintPath>.\somedll.dll</HintPath>
    </Reference>
    <PackageReference Include="Serilog" Version="2.12.0"/>
    <PackageReference Include="Serilog.Sinks.File" Version="5.0.1-dev-00947"/>
    <PackageReference Include="Serilog.Sinks.Console" Version="4.1.0"/>
    <PackageReference Include="System.ComponentModel.TypeConverter" Version="4.3.0"/>
  </ItemGroup>
</Project>

解决方案

1. 移除错误的程序集引用

错误核心是依赖版本不匹配:代码期望System.ComponentModel.TypeConverter 6.0.0.0版本,但项目手动引用了4.3.0版本。对于.NET 6,该程序集属于SDK内置组件,无需手动引用,直接删除.csproj中的以下行:

<PackageReference Include="System.ComponentModel.TypeConverter" Version="4.3.0"/>

2. 重新构建项目

在WSL终端执行以下命令,确保依赖正确对齐并重新编译:

dotnet clean
dotnet restore
dotnet build --configuration Release

3. 确保pythonnet与.NET版本兼容

升级pythonnet到最新稳定版,保证与.NET 6兼容:

pip install pythonnet --upgrade

运行Python脚本前,确认环境变量指向正确的.NET运行时:

export DOTNET_ROOT=/usr/share/dotnet
export PATH=$PATH:$DOTNET_ROOT

4. 检查第三方依赖somedll.dll兼容性

确保somedll.dll是针对.NET Core/.NET 6构建的跨平台版本,而非仅Windows的原生DLL。如果是Windows专属DLL,需替换为Linux兼容版本。

5. 验证依赖路径

将构建输出的所有文件(包括依赖DLL)放在Python脚本同一目录下,确保clr.AddReference能正确加载所有依赖程序集。

验证

重新运行测试脚本,确认TypeLoadException是否消失,能否正常调用GetTemperature()方法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 03:10:34