3

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?

2
  • Try using obj for the name instead. That's the name of the variable in the delegate type. Commented Feb 8, 2012 at 21:16
  • obj does not look well for user's DSL :). Actually if there is no way to specify name of the argument, if will just use Ping("testOne1") syntax. Commented Feb 8, 2012 at 22:28

2 Answers 2

4

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

Seems I was not clear enough. But name Message is only concern I have. My goal was exactly Message not obj or something else.
Thank you for you response anyway.
Oh so you needed it to be Message. Defining a method as Jimmy states is really the best thing you can do. Though if you're open to this, you could define a new delegate with the appropriate names as an alternative.
I ended with second solution form Jimmy. Thank you for your help guys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.