Sign in to follow this  
Toolmaker

IronPython: Embedding in C# - Not fully understanding

Recommended Posts

I'm currently trying to embed IronPython within a C# environment. I'm most likely going to use it to replace Lua that I currently use(with LuaInterface). The background: I currently use Lua and after creating the LuaVM, I load an autoexec.lua which loads various other files through an include into the VM. My application starts doing it's thing and on certain events, it'll call a function that's written in Lua through the VM. This works fine. I'm trying to accomplish the same thing with IronPython at the moment. I want to create a new ScriptEngine, expose a few functions to it(Such as an 'include' or similar command), load my autoexec.py and have that include various other files. I'm up to a point where I can load an autoexec.py(or just inject other code), but I'm not entirely sure how to proceed from here. First of all, how do I call a function in the SE instance? This is what I go up till now:
            engine = Python.CreateEngine();

            var code = "def add(a, b): return a + b";
            var src = engine.CreateScriptSourceFromString(code, Microsoft.Scripting.SourceCodeKind.InteractiveCode);
            
            var scope = engine.CreateScope();
            // How do I call add?

Ideas? Toolmaker

Share this post


Link to post
Share on other sites
Problem is: I don't want to upgrade to a BETA for the moment for this. While it is an option, it should be possible to embed IronPython without using the 4.0 framework.

I find it rather odd that the IronPython documentation on this is so lacking. I do want to use the IronPython that's compliant up to Python 2.5 but the amount of documentation on it is so lacking. Esp. for a Microsoft project.

Toolmaker

Share this post


Link to post
Share on other sites
Quote:
Original post by Toolmaker
Problem is: I don't want to upgrade to a BETA for the moment for this. While it is an option, it should be possible to embed IronPython without using the 4.0 framework.

I find it rather odd that the IronPython documentation on this is so lacking. I do want to use the IronPython that's compliant up to Python 2.5 but the amount of documentation on it is so lacking. Esp. for a Microsoft project.

Toolmaker


The book IronPython in Action by Manning Press has a couple chapters on this and provides a simple engine that will allow for embedding IronPython. Overall it is a simple process, but you have to understand the mechanics behind it.

It works just fine in .NET 3.5.

Share this post


Link to post
Share on other sites
Quote:
Original post by arbitus
The book IronPython in Action by Manning Press has a couple chapters on this and provides a simple engine that will allow for embedding IronPython. Overall it is a simple process, but you have to understand the mechanics behind it.

It works just fine in .NET 3.5.


Well, that's the problem. The documentation lacks for it, and I'm actually looking for information on the web / some background info without having to pay for it. I can read code, and understand it, but none of the code I've seen till so far actually was useful or explains the mechanics behind it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this