微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用mono调用C#中的IronPython对象

我有以下Iron Python代码.

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,PublicKeyToken=31bf3856ad364e35' or one of its dependencies.
Could not load file or assembly 'Microsoft.Scripting.Core,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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐