c# - 使用单声道从 C# 调用 IronPython 对象

标签 c# mono ironpython

我有以下 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,但我得到了一些 errors

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

IronPython 似乎需要 Microsoft.Scipting.Core,但对于 mono 我不知道该怎么做?

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

最佳答案

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

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

确保您的项目可以找到这些 DLL(目前找不到,这就是您遇到错误的原因)。我从未尝试过在 Mono 上运行 IronPython,但它 should work .

请注意,针对 .NET 4.0 的 IronPython 版本不包括(或不需要)Microsoft.Scripting.Core.dll 和 Microsoft.Scripting.ExtensionAttribute.dll,因为它们的功能已合并到 System.Core。参见 this answer了解更多详情。

关于c# - 使用单声道从 C# 调用 IronPython 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3902059/

相关文章:

python - 如何将 Iron Python 字典转换为 Python 字典

c# - 找不到 IronPython sys._getframe

c# - 如何从C#的最后一行到第一行读取文本文件

c# - 如何添加外部原生依赖dll?

c# - 在 Monotouch 中正确使用 XMLRootAttribute

c# - 从 C# 中的脚本语言获取表达式树

c# - 仅在组合框的可编辑区域捕获点击事件

c# - .net 4.5.2 和 SSL 3.0

linux - 在 Ubuntu 8.10 中安装 Mono 2.10+

c# - 固定 .NET 对象数组