我尝试使用 C# 创建一个 .dll 文件,然后使用 pythonnet 在 Python 中执行该函数。但是,我遇到以下错误:
System.TypeLoadException: Could not load type 'System.IO.Directory' from assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
环境:
Pythonnet = 3.x
Python = 3.9
净=7.0
.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Reference Include="FLEXlm_C_sharp">
<HintPath>FLEXlm_C_sharp.dll</HintPath>
</Reference>
</ItemGroup>
<Target Name="CopyLicDllAfterBuild" AfterTargets="Build">
<Exec Command="xcopy "$(ProjectDir)\lmgr11_64.dll" "$(TargetDir)" /Y /I" />
</Target>
<Target Name="CopyLicDllAfterPublish" AfterTargets="Publish">
<Exec Command="xcopy "$(ProjectDir)\lmgr11_64.dll" "$(PublishDir)" /Y /I" />
</Target>
</Project>
Python
import clr
import os
dll_directory = "./"
clr.AddReference(os.path.join(dll_directory, "LicenseService_Dll.dll"))
clr.AddReference(os.path.join(dll_directory, "FLEXlm_C_sharp.dll"))
from Verifai.Infra.Service.Repository import FlexlmLicenseService
license_service = FlexlmLicenseService()
directory_path = r"D:\LicenseService\pythonDll"
status = license_service.CheckOutUsingLicenseSettings(directory_path)
我遇到错误:“无法从程序集“System.Runtime,Version=4.0.0.0”加载类型“System.IO.Directory””。
任何解决此问题的帮助将不胜感激。
我遇到了同样的问题,并通过将 Visual Studio 更新到最新版本解决了问题