'System.Runtime.CompilerServices.Closure'异常:无法从程序集加载类型类型。 [英] Iron Python System.TypeLoadException: Could not load type 'System.Runtime.CompilerServices.Closure' from assembly 'System.Core'

查看:0
本文介绍了'System.Runtime.CompilerServices.Closure'异常:无法从程序集加载类型类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用Iron Python在C#中运行基于Python的DLL。

所以我的C#代码中有这一行:

public void Runpython(string name, string id)
{
   var engine = Python.CreateEngine();    
   
   //Get Dll
   var path = Environment.CurrentDirectory;
   var fullPath = @$"{path}PythonService.dll";
   engine.Runtime.LoadAssembly(Assembly.LoadFile(fullPath));
   
   //Get pythonservice.py file         
   var scope = engine.Runtime.ImportModule("pythonservice");

   //Get PythonService class
   var pythonService = scope.GetVariable("PythonService");

   //Run function
   var pyService = engine.Operations.CreateInstance(pythonService);
   var result = pyService.save(name, id);
            
   Console.WriteLine(result);
}

当它尝试获取我包装在DLL中的pythonservice.py文件时,异常发生在该行:var scope = engine.Runtime.ImportModule("pythonservice");

我能知道导致此问题的原因是什么吗?

我使用的是Iron Python 2.7.11,我的C#类库和控制台应用程序都是.NET Core 3.1。

谢谢。

推荐答案

您可以做同样的工作,将IronPython清空

在";C:Program FilesPython39python.exe";或任何Python环境中安装库

试试这个:

public string Run(string scriptFilePatch, string args)
{
    var psi = new ProcessStartInfo();
    psi.FileName = @"C:Program FilesPython39python.exe"; // or any python environment

    psi.Arguments = $""{scriptFilePatch}" {args}";

    psi.UseShellExecute = false;
    psi.CreateNoWindow = true;
    psi.RedirectStandardOutput = true;
    psi.RedirectStandardError = true;
    psi.StandardOutputEncoding = Encoding.UTF8;

    string errors = "", result = "";

    using (var process = Process.Start(psi))
    {
        result = process.StandardOutput.ReadToEnd();
        errors = process.StandardError.ReadToEnd();

    }
    StringWriter writer = new StringWriter();
    HttpUtility.HtmlDecode(result, writer);
    string decodedString = writer.ToString();

    return decodedString;
}

用法:

 Run("c:/code/download.py", ""imageUrl" "fileName}"");

并阅读带有以下内容的python中的参数

Python代码:

    import sys
    url = sys.argv[1] #arg {imageUrl} recived from c# code
    fileName = sys.argv[2] #arg {fileName} recived from c# code

对于将数据返回到C#代码,您应该使用以下命令:

b = any_object;
sys.stdout.buffer.write(bytearray(b,"utf-8"))

这篇关于'System.Runtime.CompilerServices.Closure'异常:无法从程序集加载类型类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆