views:

30

answers:

1

Assume I have the following code:

public static class Foo
{
    public static void Bar() {}
}

In IronPython, I would like to have:

Bar()

Without having to include the Foo on the line. Now, I know I can say:

var Bar = Foo.Bar
Bar()

But I would like to add Bar to the ScriptScope in my C# code using SetVariable. How can I do this?

+3  A: 

Create delegate to method and set in to scope.

public class Program
{
    public static void Main(string[] args)
    {
        var python = Python.CreateEngine();
        var scriptScope = python.CreateScope();
        scriptScope.SetVariable("Print", new Action<int>(Bar.Print));

        python.Execute(
            "Print(10)",
            scriptScope
            );
    }

}

public static class Bar
{
    public static void Print(int a)
    {
        Console.WriteLine("Print:{0}", a);
    }
}
desco
Works perfectly. I tip my bonnet to you.
yodaj007