在 C# 中嵌入 IronPython 的问题(缺少编译器所需的成员“Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12678365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:05:36  来源:igfitidea点击:

Problems embedding IronPython in C# (Missing Compiler required member 'Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember'

c#python.netdynamicironpython

提问by Tolga E

I'm trying to do a simple hello world to test out embedding IronPython within C# but can't seem to resolve this problem..

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

This is my C# file;

这是我的 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();
    }
}

And this is the python class;

这是python类;

import sys

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

My target .NET framework is 4.0 and I'm using IronPython 2.6..

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

I get 2 errors when I run this one is from a file called "CSC"; Error 5 Missing compiler required member

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

'Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember' C:\Users\Tolga\documents\visual studio 2012\Projects\WindowsFormsApplication1\consoleTest\CSC consoleTest

'Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember' C:\Users\Tolga\documents\visual studio 2012\Projects\WindowsFormsApplication1\consoleTest\CSC consoleTest

The other one is from C# file i created

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

Error 6 One or more types required to compile a dynamic expression cannot be found. Are you missing a reference? C:\Users\Tolga\documents\visual studio 2012\Projects\WindowsFormsApplication1\consoleTest\Program.cs 17 9 consoleTest

错误 6 无法找到编译动态表达式所需的一种或多种类型。你缺少参考吗?C:\Users\Tolga\documents\visual studio 2012\Projects\WindowsFormsApplication1\consoleTest\Program.cs 17 9 consoleTest

Here's the output from the Build

这是构建的输出

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

采纳答案by Reed Copsey

You need to add a reference to Microsoft.CSharp.dll. This provides the required types for using dynamicin C#.

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

Also, you will likely need to upgrade to IronPython 2.7[.3] or later, as there are some incompatibilities with old releases and the newer .NET frameworks.

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

回答by cod3monk3y

You will also get this error if you've included references to the wrong target assemblies. For instance, if you're building against the .Net 4.0 Full profile, you must include the IronPython assemblies from:

如果您包含对错误目标程序集的引用,您也会收到此错误。例如,如果您针对 .Net 4.0 Full 配置文件进行构建,则必须包含来自以下内容的 IronPython 程序集:

<install directory>\IronPython 2.7\Platforms\Net40

Including assemblies from the Net35directory will also result in the missing RuntimeBindererror.

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

回答by Tok'

Very old question for a still existing issue I've faced this morning using IronPython 2.7.10 in a new project.

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

The accepted answer can now be improved : instead of manually adding Microsoft.CSharp.dll, I would recommend adding the package "Microsoft.CSharp" from nuget. Portability will be improved (netstandard, net framework, netcore...).

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