mono 2.8 支持“动态”吗?关键词?

发布于 2024-09-27 05:00:55 字数 2124 浏览 6 评论 0原文

我使用书中的代码在 Mono 2.8 上测试了 IronPython 专业 IronPython p.315 清单 15-3。

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

namespace Method2
{
    class Program
    {
        static void Main(string[] args)
        {
            // Obtain the runtime.
            var IPY = Python.CreateRuntime();

            // Create a dynamic object containing the script.
            dynamic TestPy = IPY.UseFile("TestClass.py");

            // Execute the __test__() method.
            TestPy.__test__();
        }
    }
}

我看到它编译正常,并且在 Windows 7 上运行没有问题,而 mono 2.8 给出了以下错误消息。

Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 
`Microsoft.Scripting.Hosting.ScriptScope' does not contain a definition for `__test__'
at (wrapper dynamic-method) object.CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object) 
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid1
(System.Runtime.CompilerServices.CallSite,object) 
at Method2.Program.Main (string[]) 

我认为 Mono 2.8 支持具有动态关键字的 C# 4.0,但我发现 Mono 并不完全支持“dynamic”关键字。

这是 Mono 2.8 的错误吗?

添加

这是 python 脚本。

# The class you want to access externally.
class DoCalculations():

    # A method within the class that adds two numbers.
    def DoAdd(self, First, Second):

        # Provide a result.
        return First + Second

# A test suite in IronPython.
def __test__():

    # Create the object.
    MyCalc = DoCalculations()

    # Perform the test.
    print MyCalc.DoAdd(5, 10)

    # Pause after the test session.
    raw_input('\nPress any key to continue...')

这是我使用的命令

dmcs Program.cs /r:System.Core /r:IronPython.dll /r:IronPython.Modules.dll /r:Microsoft.Dynamic.dll /r:Microsoft.Scripting.dll /r:Microsoft.CSharp.dll

它编译得很好,但当我运行执行二进制文件时它仍然中断。我是否需要将所有 dll 放在执行二进制文件所在的同一目录中?

I tested IronPython on mono 2.8 with the code in the book Professional IronPython p.315 listing 15-3.

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

namespace Method2
{
    class Program
    {
        static void Main(string[] args)
        {
            // Obtain the runtime.
            var IPY = Python.CreateRuntime();

            // Create a dynamic object containing the script.
            dynamic TestPy = IPY.UseFile("TestClass.py");

            // Execute the __test__() method.
            TestPy.__test__();
        }
    }
}

I see it's compiled OK, and run without a problem on Windows 7, whereas the mono 2.8 gives me the following error message.

Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 
`Microsoft.Scripting.Hosting.ScriptScope' does not contain a definition for `__test__'
at (wrapper dynamic-method) object.CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object) 
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid1
(System.Runtime.CompilerServices.CallSite,object) 
at Method2.Program.Main (string[]) 

I thought Mono 2.8 supports C# 4.0 which has the dynamic keyword, but I see that the 'dynamic' keyword is not fully supported with mono.

Is this a bug of Mono 2.8?

ADDED

This is the python script.

# The class you want to access externally.
class DoCalculations():

    # A method within the class that adds two numbers.
    def DoAdd(self, First, Second):

        # Provide a result.
        return First + Second

# A test suite in IronPython.
def __test__():

    # Create the object.
    MyCalc = DoCalculations()

    # Perform the test.
    print MyCalc.DoAdd(5, 10)

    # Pause after the test session.
    raw_input('\nPress any key to continue...')

This is the command that I used

dmcs Program.cs /r:System.Core /r:IronPython.dll /r:IronPython.Modules.dll /r:Microsoft.Dynamic.dll /r:Microsoft.Scripting.dll /r:Microsoft.CSharp.dll

It compiles well, but it still breaks when I run the execution binary. Do I need to have all the dlls in the same directory where the execution binary locates?

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

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

发布评论

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

评论(3

守望孤独 2024-10-04 05:00:55

事实上,您收到 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException 意味着动态关键字起作用了。您遇到的问题是 IPY.UseFile("TestClass.py");返回一个看不到您的测试方法的 ScriptScope。所以问题在于你的 python 源代码或者你如何将 IronPython 包含在 mono 中。

The fact that you are getting a Microsoft.CSharp.RuntimeBinder.RuntimeBinderException means that the dynamic keyword worked. The problem you are having is that IPY.UseFile("TestClass.py"); is returning a ScriptScope that doesn't see your test method. So the problem lies with your python source or how you are including IronPython with mono.

海风掠过北极光 2024-10-04 05:00:55

使用 C# 4 配置文件时,Mono 2.8 当然支持 dynamic 关键字。

我想我的问题是你是如何构建这个样本的?

只是为了好玩,我刚刚将您的示例粘贴到 MonoDevelop 中。我必须首先告诉 MonoDevelop 使用 C# 4 而不是 C# 3.5。

显然,dynamic 关键字是在 C# 4 中引入的。

另外,我确实必须包含一些程序集引用:System.Core、IronPython.dll、IronPython.Modules.dll、Microsoft.Dynamic.dll、Microsoft.Scripting.dll 和 Microsoft.CSharp.dll。我不确定我是否需要它们。

如果您从命令行构建,则需要使用“dmcs”作为编译器(以指示 .NET 4 配置文件),并且需要包含程序集引用。

使用此配置构建没有问题(至少对我来说)。

The dynamic keyword is certainly supported by Mono 2.8 when using the C# 4 profile.

I guess my question is how you are building this sample?

Just for kicks, I just pasted your sample into MonoDevelop. I had to tell MonoDevelop to use C# 4 instead of C# 3.5 first of all.

The dynamic keyword was introduced in C# 4 obviously.

Also, I did have to include a few assembly references: System.Core, IronPython.dll, IronPython.Modules.dll, Microsoft.Dynamic.dll, Microsoft.Scripting.dll, and Microsoft.CSharp.dll. I am not sure if I needed them all.

If you are building from the command-line you need to use 'dmcs' as the compiler (to indicate the .NET 4 profile) and you need to include the assembly references.

It builds without problems with this configuration (for me at least).

影子的影子 2024-10-04 05:00:55

我正在回答有关 Mono 2.10 的问题。

我可以在 Windows 7 上从命令行成功构建并执行代码,而无需使用 MonoDevelop。步骤如下:

  1. 在 Windows 7 上安装 Mono 2.10.8
  2. 安装IronPython 2.7.2.1
  3. “C:\Program Files\Mono\bin”或等效项和“C:\Program Files\IronPython 2.7”或等效项应是系统路径的一部分。
  4. TestClass.py 和 Program.cs 应该位于同一文件夹中。
  5. 在 dos 提示符下,对应于 TestClass.py 和 Program.cs 所在的文件夹,执行 setmonopath 批处理文件。
  6. 从同一 dos 提示符执行以下命令:

dmcs Program.cs /r:System /r:"C:\Program Files\IronPython 2.7\IronPython.dll" /r:"C:\Program Files\IronPython 2.7\IronPython. Modules.dll" /r:"C:\Program Files\IronPython 2.7\Microsoft.Dynamic.dll" /r:"C:\Program Files\IronPython 2.7\Microsoft.Scripting.dll" /r:"C:\program Files\IronPython 2.7\Microsoft.Scripting.MetaData.dll" /r:Microsoft.Csharp

Program.exe 应该已成功生成,并且可以执行相同的操作而不会出现任何错误消息。

I am responding to this question with respect to Mono 2.10.

I could successfully build and execute the code from command line on Windows 7 without using MonoDevelop. Here are the steps:

  1. Install Mono 2.10.8 on Windows 7.
  2. Install IronPython 2.7.2.1.
  3. "C:\Program Files\Mono\bin" or equivalent and "C:\Program Files\IronPython 2.7" or equivalent should be part of system path.
  4. Both TestClass.py and Program.cs should be in the same folder.
  5. From the dos prompt, corresponding to the folder where TestClass.py and Program.cs exists, execute setmonopath batch file.
  6. From the same dos prompt execute the following command:

dmcs Program.cs /r:System /r:"C:\Program Files\IronPython 2.7\IronPython.dll" /r:"C:\Program Files\IronPython 2.7\IronPython.Modules.dll" /r:"C:\Program Files\IronPython 2.7\Microsoft.Dynamic.dll" /r:"C:\Program Files\IronPython 2.7\Microsoft.Scripting.dll" /r:"C:\program Files\IronPython 2.7\Microsoft.Scripting.MetaData.dll" /r:Microsoft.Csharp

Program.exe should be successfully generated and the same can be executed without any error message.

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