[IronPython] IP 2.0 Hosting

Toni Alatalo antont at kyperjokki.fi
Mon Dec 17 10:03:57 CET 2007


On Monday 17 December 2007 03:52:03 Slide wrote:
> I am at a huge loss trying to figure out hosting IP 2.0 in my C#
> application to provide scripting capabilities to my app. Does ANYONE
> have an example of this? I tried grabbing the document listed in a
> post a little while ago, but the class names in the document don't
> match anything I have in the package I downloaded. I'm looking for the

I have done it for two systems now.

Apparently that document is not about the implementation in the 2.0 alphas, 
but I've guessed that of a plan for the next step.

The embeddings I made were actually done before that document was sent, and I 
just mostly did trial-and-error based on the method names and signatures etc. 
Don't have the code here now, but unless someone sends something soon can put 
something together later today (in 6 hours or so) .. it's simple, nothing 
special there.

> ability to have a class in Python inherit (implement) an interface
> defined in C# and then create an instance of that class for use from
> the C#.

Python uses 'duck typing', so to implement an interface you just implement it, 
i.e. define the class so that it has the methods that the interface requires. 
I actually don't know if that is the case when giving objects to c#-written 
code that expects something that implements a certain interface, but based on 
how I've used delegates my guess is that no extra syntax is needed. For 
example if there is a delegate type MyDelegate(int i); defined in c#, a 
normal Python function that takes 1 argument is accepted 
(ironpython/dlr/something seems to introspect the definition somehow at 
runtime and raises an error if the signature does not match).
 
I have not yet found a way to expose selected c#-written classes (or 
interfaces) to the embedded Python context - have tried many things with the 
context and module etc. creation methods that are there in 2.0 alphas with no 
luck.

What works, and what I've resorted to currently, is just adding references to 
the dlls also on the Python side, and importing the modules/classes in 
Python. Typechecking of objects created from those assemblies seems to work 
back on the c#-written side as long as the path from where you loaded the dll 
is exactly the same.

With 1.1 it is possible to use the LoadAssembly command to make already loaded 
assemblies from c# to show in the Python side. I am guessing that the 2.x 
series will have something similar using the runtime objects or what was it 
that is mentioned in that document, but there was no detailed info of that in 
the doc yet and I have not found any implementation of it either (i have not 
checked out stuff from codeplex repo though, just used alpha releases).

> slide

~Toni



More information about the Ironpython-users mailing list