[IronPython] Need help using IronPython from C#
Jason Ferrara
jason.ferrara at jacquette.com
Sat Aug 12 20:00:36 CEST 2006
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;
IronPython.Runtime.Operations.Ops.TryGetAttr
(pythonObject, IronPython.Runtime.SymbolTable.StringToId
("GetNumberOfEntries"), out method);
GetNumberOfEntries = (GetNumberOfEntriesDelegate)
IronPython.Runtime.Operations.Ops.GetDelegate(method, typeof
(GetNumberOfEntriesDelegate));
IronPython.Runtime.Operations.Ops.TryGetAttr
(pythonObject, IronPython.Runtime.SymbolTable.StringToId("GetEntry"),
out method);
GetEntry = (GetEntryDelegate)
IronPython.Runtime.Operations.Ops.GetDelegate(method, typeof
(GetEntryDelegate));
IronPython.Runtime.Operations.Ops.TryGetAttr
(pythonObject, IronPython.Runtime.SymbolTable.StringToId("close"),
out method);
close = (closeDelegate)
IronPython.Runtime.Operations.Ops.GetDelegate(method, typeof
(closeDelegate));
}
}
More information about the Ironpython-users
mailing list