使用 mono 从 C# 调用 IronPython 对象

发布于 2024-09-27 01:04:19 字数 1814 浏览 3 评论 0原文

我有以下 IronPython 代码。

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

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

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);
    }
}

复制 IronPython.dll 后,我运行以下命令。 (我尝试运行 gacutil,但出现一些错误.

 dmcs /r:IronPython.dll callipy.cs

如下所示。

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.
...

我收到了一些错误消息,

  • 但 那?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2024-10-04 01:04:19

IronPython 不是一个独立的 DLL。它有一些应该随 IronPython 一起提供的依赖项(它们包含在针对 .NET 2.0 的最新压缩发行版中——请参阅 IronPython 下载页面):

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文