IronPython tutorial

. Getting Started

1.1. Introduction

The simplest way to get starded with embedding IronPython in your application is to use the Python class defined in IronPython.Hosting. This class includes functions for creating IronPython script engines and accessing commonly used Python-specific functionality. Much of the functionality is exposed as .NET extension methods so that it seamlessly extends the normal DLR hosting API surface.

Most of the functions in the Python class have overloads that operate on either a ScriptRuntime or a ScriptEngine. These functions provide identical functionality but both forms are provided for convenience for when you may be working with either an engine or an entire runtime.

1.2. Samples

1.2.1. Hello World

The simplest program to start off with is always Hello World. Here is a simple Hello World app that creates a Python engine from C#:

using System;
using IronPython.Hosting;

public class HelloWorld {
    public static void Main(string[] args) {
        var engine = Python.CreateEngine();
        engine.CreateScriptSourceFromString("print 'hello world'").Execute();
    }
}
Compile using:
csc HelloWorld.cs /r:IronPython.dll /r:Microsoft.Scripting.dll

Here’s the equivalent program in VB.NET:

Imports System
Imports IronPython.Hosting

Public Class HelloWorld
    Public Shared Sub Main(ByVal args As String())
        Python.CreateEngine.CreateScriptSourceFromString("print 'hello world'").Execute
    End Sub
End Class
Compile using:
vbc HelloWorld.vb /r:IronPython.dll /r:Microsoft.Scripting.dll /r:Microsoft.Scripting.Core.dll

And again in in IronPython:

import clr
clr.AddReference('IronPython')
from IronPython.Hosting import Python

engine = Python.CreateEngine()
engine.CreateScriptSourceFromString("print 'hello world'").Execute()

1.2.2. Calling Python from C#

This sample demonstrates how to get functions and classes defined in Python and access them from your .NET application.

In order to get back the results of executed Python code we need to first create a ScriptScope in which we can execute the code. We then execute the code we’re interested inside of that scope and fetch the values back out. To get the values back out we use the GetVariable method defined on ScriptScope. There is both a generic version of this method as well as a non-generic version - here we use the generic version which allows us to perform conversions on the the result of the variable. One of the most convenient conversions for functions and classes is to convert these objects to delegates. This allows us to repeatedly call the same function or class with different arguments.

C# Example:

using System;
using IronPython.Hosting;

public class CallingPython {
    public static void Main(string[] args) {
        var engine = Python.CreateEngine();
        var scope = engine.CreateScope();
        var source = engine.CreateScriptSourceFromString(
            "def adder(arg1, arg2):\n" +
            "   return arg1 + arg2\n"  +
            "\n" +
            "class MyClass(object):\n" +
            "   def __init__(self, value):\n" +
            "       self.value = value\n");
        source.Execute(scope);

        var adder = scope.GetVariable<Func<object, object, object>>("adder");
        Console.WriteLine(adder(2, 2));
        Console.WriteLine(adder(2.0, 2.5));

        var myClass = scope.GetVariable<Func<object, object>>("MyClass");
        var myInstance = myClass("hello");

        Console.WriteLine(engine.Operations.GetMember(myInstance, "value"));
    }
}
原文地址:https://www.cnblogs.com/lexus/p/2816400.html