44

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

This is my C# file;

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;

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

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

'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

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

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 ==========
Tolga E
  • 12,188
  • 15
  • 49
  • 61

3 Answers3

120

You need to add a reference to Microsoft.CSharp.dll. This provides the required types for using dynamic in 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.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • When I click references in my project, I see Microsoft.CSharp in the list – Tolga E Oct 01 '12 at 17:58
  • @TolgaE If you're compiling from the command line, I'd recommend using `MSBuild` with the project, as this will get the references. If you use CSC, you need to specify the references yourself (it won't read from the project file). – Reed Copsey Oct 01 '12 at 18:01
  • @ToglaE Otherwise, just build it inside of Visual Studio, and it should work (since that reference exists) – Reed Copsey Oct 01 '12 at 18:01
  • I'm building it within Visual Studio. I just hit the "Start" button – Tolga E Oct 01 '12 at 18:02
  • @ToglaE Make sure you're targetting .NET 4.5 or .NET 4.0 Full (not client profile) in the build settings. Does that help? – Reed Copsey Oct 01 '12 at 18:04
  • thanks for the suggestions but it's set to 4.0. I tried with 4.5 as well – Tolga E Oct 01 '12 at 18:05
  • I added the build output in the question, in case that helps – Tolga E Oct 01 '12 at 18:09
  • @TolgaE I believe you may need to upgrade to the latest IronPython (2.7.3) as there are likely some incompatibilities with 2.6 and the latest framework. – Reed Copsey Oct 01 '12 at 18:11
  • Huzzah!!! That worked (I downgraded to 2.6 before for another problem that's not relevant right now. Thank you! – Tolga E Oct 01 '12 at 18:13
  • 1
    I'm getting this same compiler error (Missing compiler required member) running IronPython 2.7.4 and targeting .NET 4.0 Full Profile. – cod3monk3y Dec 10 '13 at 18:01
  • 1
    Solved my issue. Since it solves a different aspect than this answer, I've added a secondary answer and linked to the question which helped to resolve my issue. – cod3monk3y Dec 10 '13 at 18:19
  • FWIW adding Microsoft.CSharp.dll means var scriptOptions = ScriptOptions.Default.WithReferences("Microsoft.CSharp") i.e. drop the dll. Stumped me for a few mins :) – Jon H Apr 06 '16 at 13:03
3

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:

<install directory>\IronPython 2.7\Platforms\Net40

Including assemblies from the Net35 directory will also result in the missing RuntimeBinder error.

Community
  • 1
  • 1
cod3monk3y
  • 9,508
  • 6
  • 39
  • 54
0

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

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

Tok'
  • 565
  • 8
  • 11