【问题标题】:Problems embedding IronPython in C# (Missing Compiler required member 'Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember'在 C# 中嵌入 IronPython 时出现问题(缺少编译器所需的成员 'Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember'
【发布时间】:2023-03-05 10:20:01
【问题描述】:

我正在尝试做一个简单的 hello world 来测试在 C# 中嵌入 IronPython,但似乎无法解决这个问题..

这是我的 C# 文件;

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

public class dynamic_demo
{
    static void Main()
    {
        var ipy = Python.CreateRuntime();

        dynamic test = ipy.UseFile(@"../../Test.py");

        test.Simple();
    }
}

这是python类;

import sys

def Simple():
    print 'Hello from Python'
    print "Call Dir(): "
    print dir()
    print "Print the Path: " 
    print sys.path

我的目标 .NET 框架是 4.0,我正在使用 IronPython 2.6..

当我运行这个错误来自一个名为“CSC”的文件时,我得到了 2 个错误; 错误 5 缺少编译器所需的成员

'Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember' C:\Users\Tolga\documents\visual 工作室 2012\Projects\WindowsFormsApplication1\consoleTest\CSC 控制台测试

另一个来自我创建的 C# 文件

错误 6 编译动态表达式需要一种或多种类型 找不到。你错过了一个 参考? C:\Users\Tolga\documents\visual studio 2012\Projects\WindowsFormsApplication1\consoleTest\Program.cs 17 9 控制台测试

这是构建的输出

1>------ Build started: Project: consoleTest, Configuration: Debug Any CPU ------
1>CSC : warning CS1685: The predefined type 'System.Runtime.CompilerServices.CallSite' is defined in multiple assemblies in the global alias; using definition from 'c:\Program Files (x86)\IronPython 2.6\Microsoft.Scripting.Core.dll'
1>CSC : warning CS1685: The predefined type 'System.Runtime.CompilerServices.CallSite' is defined in multiple assemblies in the global alias; using definition from 'c:\Program Files (x86)\IronPython 2.6\Microsoft.Scripting.Core.dll'
1>CSC : warning CS1685: The predefined type 'System.Runtime.CompilerServices.CallSiteBinder' is defined in multiple assemblies in the global alias; using definition from 'c:\Program Files (x86)\IronPython 2.6\Microsoft.Scripting.Core.dll'
1>CSC : warning CS1685: The predefined type 'System.Runtime.CompilerServices.ExtensionAttribute' is defined in multiple assemblies in the global alias; using definition from 'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll'
1>CSC : error CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember'
1>C:\Users\Tolga\documents\visual studio 2012\Projects\WindowsFormsApplication1\consoleTest\Program.cs(17,9,17,20): error CS1969: One or more types required to compile a dynamic expression cannot be found. Are you missing a reference?
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

【问题讨论】:

    标签: c# python .net dynamic ironpython


    【解决方案1】:

    您需要添加对Microsoft.CSharp.dll 的引用。这提供了在 C# 中使用 dynamic 所需的类型。

    此外,您可能需要升级到 IronPython 2.7[.3] 或更高版本,因为旧版本和较新的 .NET 框架存在一些不兼容性。

    【讨论】:

    • 当我在我的项目中单击引用时,我会在列表中看到 Microsoft.CSharp
    • @TolgaE 如果您从命令行编译,我建议在项目中使用MSBuild,因为这将获得引用。如果使用 CSC,则需要自己指定引用(不会从项目文件中读取)。
    • @ToglaE 否则,只需在 Visual Studio 中构建它,它应该可以工作(因为该引用存在)
    • 我在运行 IronPython 2.7.4 并针对 .NET 4.0 Full Profile 时遇到了同样的编译器错误(缺少编译器所需的成员)。
    • 解决了我的问题。由于它解决了与此答案不同的方面,因此我添加了辅助答案并链接到有助于解决我的问题的问题。
    【解决方案2】:

    如果您有included references to the wrong target assemblies,您也会收到此错误。例如,如果您要针对 .Net 4.0 完整配置文件进行构建,则必须包含以下 IronPython 程序集:

    <install directory>\IronPython 2.7\Platforms\Net40
    

    包含Net35 目录中的程序集也会导致缺少RuntimeBinder 错误。

    【讨论】:

      【解决方案3】:

      今天早上我在一个新项目中使用 IronPython 2.7.10 时遇到的一个仍然存在的问题,这是一个非常老的问题。

      现在可以改进接受的答案:我建议不要手动添加 Microsoft.CSharp.dll,而是从 nuget 添加包“Microsoft.CSharp”。可移植性将得到改进(netstandard、net framework、netcore...)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-28
        • 2011-05-20
        • 1970-01-01
        • 1970-01-01
        • 2017-04-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多