6

I'm doing the example that can be found here. So I'm trying to run IronPython in a C# script:

Python:

def hello(name):
    print "Hello " + name + "! Welcome to IronPython!"
    return

def add(x, y):
    print "%i + %i = %i" % (x, y, (x + y))
    return

def multiply(x, y):
    print "%i * %i = %i" % (x, y, (x * y))
    return

C#:

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

namespace IntroIronPython
{
    class IronPythonMain
    {
        static void Main(string[] args)
        {
            // Create a new ScriptRuntime for IronPython
            Console.WriteLine("Loading IronPython Runtime...");
            ScriptRuntime python = Python.CreateRuntime();

            try
            {
                // Attempt to load the python file
                Console.WriteLine("Loading Python File...");
                // Create a Dynamic Type for our Python File
                dynamic pyfile = python.UseFile("PythonFunctions.py");
                Console.WriteLine("Python File Loaded!");

                Console.WriteLine("Running Python Commands...\n");

                // Call the hello(name) function
                pyfile.hello("Urda");
                …

And from here I have this error: "Dynamic Operation cannot be compiled without "Microsoft.CSharp.dll" assembly reference." And I seriously don't understand what that is about, what did I forget to add?

In my references I have: References of my project

Thx for your help.

Ps: I'm on MonoDevelop.

Urda
  • 5,460
  • 5
  • 34
  • 47
ssx
  • 184
  • 1
  • 4
  • 13

3 Answers3

12

This is what helped me. I am using Xamarian Studio v5.8.1 (build 8) to write a C# program. I just had to right click "References" -> "Edit References" -> started typing "Microsoft" in the search bar -> Checked the box next to "Microsoft.CSharp" -> and clicked "OK".

I just saved and ran the program after that - everything works as expected!

tylerlindell
  • 1,545
  • 2
  • 17
  • 20
4

Ok. Basically, my mistake was linked to the fact that I added my IronPython assemblies from the wrong platform. Verify that:

  • Target Framework: 4.0

  • Add all the assemblies provided by IronPython in [IronPython-2.7.3]->[Platforms]->[Net40].

Thx to everyone who gave me advices.

Ps:Now, of course, there is another problem… But it's not about that topic anymore.

ssx
  • 184
  • 1
  • 4
  • 13
  • 1
    Make sure you flag your own answer as the solution so future visitors can see you solved the problem! Also I'm glad you found and chose to use my example :) – Urda Jul 12 '12 at 18:57
  • This was my problem as well. I had added my references using the ".NET" tab in Visual Studio C# 2010 Express. I think the original files that appeared may only have been from the `Net35` directory. Be aware of the `Path` in the Add References dialogue box. When I added them from `IronPython 2.7\Platforms\Net40` the build succeeded, and both sets of assemblies now appear on the .Net tab in Add References. (This may have been the case before too, I just didn't notice). – cod3monk3y Dec 10 '13 at 18:10
2

Microsoft.CSharp.dll contains the dynamic portion of the C# compiler. Any time you use dynamic in your code, you need a reference to it. I'm not familiar with MonoDevelop, but you'll probably have to set the target framework to 4.0, and add a reference to Microsoft.CSharp.dll to the project (probably right-click on the project, 'Add Reference', find Microsoft.CSharp).

My guess is that the project you're using was created with a pre-release version of VS, and is missing the proper references.

Jeff Hardy
  • 7,632
  • 24
  • 24
  • Hi, this is exactly what I have: Framework 4.0, and as you see in the image Microsoft.CSharp is already in my references; that's the reason why I don't understand this error. Microsoft.CSharp is added in the reference and it stills saying that I cannot compile without it… Do you think other assemblies should be also added to make it work? – ssx Jul 10 '12 at 20:15
  • Oh, I missed that in the image. I'm not sure what's going on then. It might be a MonoDevelop issue. – Jeff Hardy Jul 11 '12 at 18:08
  • You can find my solution bellow. I think I was adding the wrong assemblies from the from platform. Got no such problems with IronPython 2.7.0 so I switched. – ssx Jul 12 '12 at 15:14