使用单声道从 C# 调用 IronPython 对象 [英] Calling IronPython object from C# with mono

查看:17
本文介绍了使用单声道从 C# 调用 IronPython 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 IronPython 代码.

类你好:def __init__(self):经过定义添加(自我,x,y):返回 (x+y)

我需要从 C# 中调用它,我想出了以下代码.

使用系统;使用 IronPython.Hosting;使用 IronPython.Runtime;使用 IronPython;使用 Microsoft.Scripting.Hosting;使用 Microsoft.Scripting;类你好{公共静态无效 Main(){ScriptEngine 引擎 = Python.CreateEngine();ScriptSource script = engine.CreateScriptFromSourceFile("myPythonScript.py");ScriptScope 范围 = engine.CreateScope();脚本.执行(范围);}}

复制 IronPython.dll 后,我运行以下命令.(我尝试运行 gacutil,但遇到了一些 错误.

<上一页>dmcs/r:IronPython.dll callipy.cs

但是我收到了一些错误信息,如下所示.

<上一页>无法加载文件或程序集Microsoft.Scripting.ExtensionAttribute,Version=2.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"或其依赖项之一.程序集/Users/smcho/Desktop/cs/namespace/IronPython.dll 中缺少方法 .ctor,键入 System.Runtime.CompilerServices.ExtensionAttribute找不到自定义 attr 构造函数图像:/Users/smcho/Desktop/cs/namespace/IronPython.dll mtoken: 0x0a000080无法加载文件或程序集Microsoft.Scripting.Core,Version=1.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"或其依赖项之一.无法加载文件或程序集Microsoft.Scripting.Core,Version=1.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"或其依赖项之一.无法加载文件或程序集Microsoft.Scripting.Core,Version=1.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"或其依赖项之一....

看来 IronPython 需要 Microsoft.Scipting.Core,但是单声道我不知道该怎么办?

  • C# 可以在 Mono 上运行 IronPython 对象吗?如果是,该怎么做?

解决方案

IronPython 不是独立的 DLL.它有一些本应与 IronPython 一起提供的依赖项(它们包含在针对 .NET 2.0 的最新压缩分发中 - 请参阅 IronPython 下载页面):

IronPython.Modules.dllMicrosoft.Dynamic.dllMicrosoft.Scripting.dllMicrosoft.Scripting.Core.dllMicrosoft.Scripting.Debugging.dllMicrosoft.Scripting.ExtensionAttribute.dll

确保您的项目可以找到这些 DLL(目前无法找到,这就是您遇到错误的原因).我从未尝试在 Mono 上运行 IronPython,但它应该可以工作.

请注意,面向 .NET 4.0 的 IronPython 版本不包括(或不需要)Microsoft.Scripting.Core.dll 和 Microsoft.Scripting.ExtensionAttribute.dll,因为它们的功能已合并到 System.Core.有关详细信息,请参阅此答案.

I have the following IronPython code.

class Hello:
    def __init__(self):
        pass
    def add(self, x, y):
        return (x+y)

I need to call this from C#, and I came up with the following code.

using System;
using IronPython.Hosting;
using IronPython.Runtime;
using IronPython;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting;

class Hello {
    public static void Main()
    {
        ScriptEngine engine = Python.CreateEngine();
        ScriptSource script = engine.CreateScriptFromSourceFile("myPythonScript.py");
        ScriptScope scope = engine.CreateScope();

        script.Execute(scope);
    }
}

After copying IronPython.dll, I run the following command. (I tried to run gacutil, but I got some errors.

 dmcs /r:IronPython.dll callipy.cs

But I got some error messages as follows.

Could not load file or assembly 'Microsoft.Scripting.ExtensionAttribute, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies.
Missing method .ctor in assembly /Users/smcho/Desktop/cs/namespace/IronPython.dll, type System.Runtime.CompilerServices.ExtensionAttribute
Can't find custom attr constructor image: /Users/smcho/Desktop/cs/namespace/IronPython.dll mtoken: 0x0a000080
Could not load file or assembly 'Microsoft.Scripting.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies.
Could not load file or assembly 'Microsoft.Scripting.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies.
Could not load file or assembly 'Microsoft.Scripting.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies.
...

It seems like that IronPython needs Microsoft.Scipting.Core, but with mono I don't know what to do?

  • Can C# run IronPython object on Mono? If so, how to do that?

解决方案

IronPython is not a stand-alone DLL. It has some dependencies which should have been shipped with IronPython (they are included in the latest zipped distribution targeting .NET 2.0 -- see the IronPython download page):

IronPython.Modules.dll
Microsoft.Dynamic.dll
Microsoft.Scripting.dll
Microsoft.Scripting.Core.dll
Microsoft.Scripting.Debugging.dll
Microsoft.Scripting.ExtensionAttribute.dll

Make sure your project can find these DLLs (which it currently can't, which is why you're getting errors). I've never tried running IronPython on Mono, but it should work.

Note that the IronPython release targeting .NET 4.0 doesn't include (or need) Microsoft.Scripting.Core.dll and Microsoft.Scripting.ExtensionAttribute.dll, since their functionality has been merged into System.Core. See this answer for more details.

这篇关于使用单声道从 C# 调用 IronPython 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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