Question

So I am hosting IronPython in my C# application. IronPhyton is used to implement a DSL for users. The DSL syntax should be something like this:

Ping(Message = "testOne1")

The hosting code looks like:

var engine = Python.CreateEngine();
var scope = engine.CreateScope();

Action<string> ping = (message) => Console.WriteLine(message.ToString());            
scope.SetVariable("Ping", ping);
var script = @"
Ping(Message = ""testOne1"")
";
engine.Execute(script, scope);

But this does not work because Action<string> does not keep name of the argument. Calling it without the parameter name works as expected:

Ping("testOne1")

How do I store a function and call it with named arguments?

Was it helpful?

Solution

To use named arguments you'll have to define the method statically. For example, I'll just put all DSL operations into an Operations static class.

public static class Operations {
  public static void Ping(string Message) {
    Console.WriteLine(Message);
  }
}

Then named arguments will work:

var engine = Python.CreateEngine();
var scope = engine.CreateScope();

// Load the assembly where the operations are defined.
engine.Runtime.LoadAssembly(Assembly.GetExecutingAssembly());

// Import the operations modules, settings their names as desired.
engine.Execute(@"
from Operations import Ping
", scope);

// Now named arguments will work...
var script = @"
Ping(Message = ""Ping!"")
";

engine.Execute(script, scope);

Now if I could give you some advise; I'd prefer to implement the actual Python API in Python, and have that call back into my .NET code as needed. For example, instead of having the "operations" defined in C#, you'd have an Operations.py file which defines your Python DSL:

# Get access to your .NET API
import clr
clr.AddReference("MyAPI")
import MyAPI

# Define the Ping call to call into your .NET API
def Ping(Message):
  MyAPI.Ping(Message)

And your hosting code doesn't need to change at all.

Both are valid solutions, but the last one lets you iterate on your DSL easily.

Good luck!

OTHER TIPS

The name of the parameter is defined by the name provided in the delegate type. In the case of Action<T>, the parameter name is obj.

public delegate void Action<in T>(
    T obj
)

obj should work for you. Are you sure it isn't working? It works for me.

In an IronPython project I have a library:

namespace TestLibrary
{
    public static class Test
    {
        public static readonly Action<string> WriteLine =
            msg => Console.WriteLine(msg);

        // the same works if I do this instead
        //public static readonly Action<string> WriteLine = Console.WriteLine;
    }
}

And this works:

from TestLibrary import Test

#Test.WriteLine(msg='foo') # error 
Test.WriteLine(obj='foo') # works

Hosted, same deal:

var engine = Python.CreateEngine();
dynamic scope = engine.CreateScope();

Action<string> writeLine = msg => Console.WriteLine(msg);
// or even
//Action<string> writeLine = Console.WriteLine;
scope.writeLine = writeLine;

//engine.Execute("writeLine(msg='foo')", scope); // error
engine.Execute("writeLine(obj='foo')", scope); // works
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top