[IronPython] Need help using IronPython from C#

Kristof Wagemans kristof.wagemans at gmail.com
Sat Aug 12 23:12:14 CEST 2006

A while ago I asked a similar question about how to get to methods inside
the engine from C#. This was the sample I got:

public delegate int IntIntDelegate(int arg);
engine.Execute("def IntIntMethod(a): return a * 100");

IntIntDelegate d = engine.Evaluate<IntIntDelegate>("IntIntMethod");

Console.WriteLine(d(2)); // This prints "200"

You can try to adapt this and see if it works for you. It looks a bit easier
this way.

-----Original Message-----
From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of Jason Ferrara
Sent: Saturday 12 August 2006 20:01
To: Discussion of IronPython
Subject: Re: [IronPython] Need help using IronPython from C#

On Aug 11, 2006, at 5:32 PM, Dino Viehland wrote:

> You're quickly getting into the guts of the runtime, so you'll find  
> most of what you want over in Ops.
> To get an attribute off of an object you can do TryGetAttr(object  
> o, SymbolId name, out object ret).  o would be your ls, SymbolId's  
> our what we use internally to represent attributes - you can get  
> one by doing SymbolTable.SymbolToId("foo"), and then we'll pass you  
> out the value.
> As you get the values back you'd need to re-create delegates to  
> them.  Note if you're re-creating delegates of the same type we  
> won't need to do any additional code gen work - we'll just bind the  
> new object back to the existing method we created, so this  
> shouldn't be too nasty to do.  If you're getting a property we'll  
> return the property value from Ops.GetAttr (basically if you hit a  
> descriptor we will run the descriptor code for you).  And if you're  
> getting some plain old attribute back out then you can do whatever  
> you want w/ the value from there.

Ah. Thanks. This is what I needed.  So to make a C# wrapper for a  
Python class I should do something like...

     class LogViewerService
         public delegate int GetNumberOfEntriesDelegate();
         public GetNumberOfEntriesDelegate GetNumberOfEntries;

         public delegate object GetEntryDelegate(int entryNumber);
         public GetEntryDelegate GetEntry;

         public delegate void closeDelegate();
         public closeDelegate close;

         public LogViewerService(object pythonObject)
             object method;
(pythonObject, IronPython.Runtime.SymbolTable.StringToId 
("GetNumberOfEntries"), out method);
             GetNumberOfEntries = (GetNumberOfEntriesDelegate) 
IronPython.Runtime.Operations.Ops.GetDelegate(method, typeof 
(pythonObject, IronPython.Runtime.SymbolTable.StringToId("GetEntry"),  
out method);
             GetEntry = (GetEntryDelegate) 
IronPython.Runtime.Operations.Ops.GetDelegate(method, typeof 
(pythonObject, IronPython.Runtime.SymbolTable.StringToId("close"),  
out method);
             close = (closeDelegate) 
IronPython.Runtime.Operations.Ops.GetDelegate(method, typeof 



users mailing list
users at lists.ironpython.com

More information about the Ironpython-users mailing list