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

使用Python.NET调用依赖SixLabors.ImageSharp的C# DLL报错求助

Python.NET调用依赖SixLabors.ImageSharp的C# DLL时的类型加载异常问题

我在使用Python.NET调用依赖SixLabors.ImageSharp(v3.1.3)的.NET 7.0类库DLL时遇到问题:测试类的RunTest1能正常打印内容,RunTest2可正常返回null,但RunTest3创建Image<Rgba32>实例时抛出类型加载异常。

C#测试类代码

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

namespace PythonNetTest
{
    public class TestClass
    {
        public void RunTest1()
        {
            Console.WriteLine("Test PythonNet");
        }

        public Image RunTest2()
        {
            return null;
        }

        public Image RunTest3()
        {
            return new Image<Rgba32>(100, 100);
        }
    }
}

Python加载DLL代码

import os
import sys
import clr

from System.Reflection import Assembly
from System.IO import File
from System import Activator

clr.AddReference(r'<Path to ImageSharp DLL>\SixLabors.ImageSharp.dll')

path = r'<Path to C# dll>\PythonNetTest.dll'

dll = Assembly.Load(File.ReadAllBytes(path))

obj_type = dll.GetType('PythonNetTest.TestClass')

obj = Activator.CreateInstance(obj_type)

obj.RunTest1()

v2=obj.RunTest2()

v3=obj.RunTest3()

抛出的异常信息

TypeLoadException: Could not load type 'System.Environment' from assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
   at SixLabors.ImageSharp.Configuration..ctor(IImageFormatConfigurationModule[] configurationModules)
   at SixLabors.ImageSharp.Configuration.CreateDefaultInstance()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at SixLabors.ImageSharp.Configuration..cctor()


The above exception was the direct cause of the following exception:

Traceback (most recent call last):

  File ~\miniconda3\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\<***>\documents\untitled0.py:31
    v3=obj.RunTest3()

TypeInitializationException: The type initializer for 'SixLabors.ImageSharp.Configuration' threw an exception. ---> System.TypeLoadException: Could not load type 'System.Environment' from assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
   at SixLabors.ImageSharp.Configuration..ctor(IImageFormatConfigurationModule[] configurationModules)
   at SixLabors.ImageSharp.Configuration.CreateDefaultInstance()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at SixLabors.ImageSharp.Configuration..cctor()
   --- End of inner exception stack trace ---
   at SixLabors.ImageSharp.Image`1..ctor(Int32 width, Int32 height)
   at PythonNetTest.TestClass.RunTest3()

异常原因

这个异常的核心原因是**.NET Core/.NET 7的程序集绑定与Python.NET默认依赖的.NET Framework程序集版本不兼容**:

  1. SixLabors.ImageSharp v3.1.3基于.NET Standard 2.0/.NET Core/.NET 7编译,它引用的System.Environment属于.NET Core体系下的System.Runtime程序集(版本通常为5.0+/7.0+)。
  2. Python.NET默认优先加载.NET Framework的程序集,而.NET Framework中System.Environment实际属于mscorlib程序集,并非System.Runtime v4.0.0.0。当ImageSharp尝试从错误的程序集版本中加载System.Environment类型时,自然找不到匹配项,触发TypeLoadException。
  3. RunTest1和RunTest2未触发ImageSharp的类型初始化逻辑,因此不会暴露问题;但RunTest3创建Image实例时,会触发SixLabors.ImageSharp.Configuration的静态构造函数,该构造函数调用了Environment.ProcessorCount等方法,进而触发了错误的程序集绑定逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 04:17:02