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

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

问题描述

我有以下IronPython代码.

I have the following IronPython code.

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

我需要从C#调用它,然后我想到了以下代码.

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

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

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

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

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

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

推荐答案

IronPython不是独立的DLL.它具有一些应该与IronPython一起提供的依赖项(它们包含在针对.NET 2.0的最新压缩发行版中-请参见

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

确保您的项目可以找到这些DLL(当前无法找到这些DLL,这就是为什么会出现错误).我从未尝试过在Mono上运行IronPython,但是应该可以工作.

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.

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

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.

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

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