[IronPython] Inheriting from a C# type in IronPython - is this even possible?

Kevin Gadd kevin.gadd at gmail.com
Sat Oct 7 01:12:26 CEST 2006

Blech. The lack of any (that I can find) documentation whatsoever on this
subject and lack of any easy way to debug what's actually happening has
resulted in me having to resort to using the clunky jscript.net hosting api.
If anyone ever gets a chance to take a glance at this and guess at what the
problem might be, I'd appreciate it.

On 9/26/06, Kevin Gadd <kevin.gadd at gmail.com> wrote:
> So I have an object model defined in C#, that defines various types. One
> of them is a base type called 'Element' that is designed to be inherited
> from.
> I have a class called ElementFactory that loads up one or more python
> scripts into an IronPython.Hosting engine, and exposes my object model to
> those scripts. It then tries to instantiate classes that are defined in the
> scripts.
> I have a test script that looks like this:
> from Designer.Schema import Element
> class TextStr(Element):
>     def __new__():
>         System.Windows.Forms.MessageBox.Show("__new__0")
>     def __new__(self):
>         System.Windows.Forms.MessageBox.Show("__new__1")
>     def __new__(self, a2):
>         System.Windows.Forms.MessageBox.Show("__new__2")
>     def __new__(self, a2, a3):
>         System.Windows.Forms.MessageBox.Show("__new__3")
>     def __new__(self, a2, a3, a4):
>         System.Windows.Forms.MessageBox.Show("__new__4")
>     def __init__():
>         System.Windows.Forms.MessageBox.Show("__init__0")
>     def __init__(self):
>         System.Windows.Forms.MessageBox.Show ("__init__1")
>     def __init__(self, a2):
>         System.Windows.Forms.MessageBox.Show("__init__2")
>     def __init__(self, a2, a3):
>         System.Windows.Forms.MessageBox.Show("__init__3")
>     def __init__(self, a2, a3, a4):
>         System.Windows.Forms.MessageBox.Show("__init__4")
> While I can locate the definition of the TextStr type (by finding UserType
> objects in module.Globals) and can get an instance of something that seems
> to be a TextStr (by using UserType.AllocateObject), I'm not sure it's
> actually working. The returned object is named 'Element_1', has a __type__
> member that says 'TextStr' and it is a valid Element that runs the inherited
> constructor when created. But none of the __new__ or __init__ overloads I
> defined are being run when the object is created, which implies that
> something strange is going on. I can't figure out what this Element_1 object
> actually is or why calling UserType.AllocateObject isn't doing what I
> expect it to do. The only other way I can find in the API to construct a
> python type dynamically is using Evaluate, which is not a usable solution
> for my circumstances, since when I load each of these scripts in as modules,
> they aren't defined in the default module and can't be seen by expressions
> passed into Evaluate.
> Am I just misunderstanding how this stuff works? Is it not possible to
> derive from a C# type in IronPython?
> Here is the basic boilerplate code I'm using, trimmed for space:
>         public bool LoadScript(string filename) {
>                 FileInfo info = new FileInfo(filename);
>                 OptimizedEngineModule module =
> Engine.CreateOptimizedModule(filename, info.Name.Replace(".py", ""),
> true);
>                 module.Execute();
>                 EnumTypes(module.Globals);
>                 return true;
>         }
>         public void EnumTypes(IDictionary<string, object> globals) {
>             foreach (KeyValuePair<string, object> pair in globals) {
>                 UserType type = pair.Value as UserType;
>                 if (type != null)
>                     if (!Types.ContainsKey(type.Name))
>                         Types.Add(type.Name, type);
>             }
>         }
>         public Element Construct(string typename, Document document,
> string id) {
>                 UserType type = Types[typename];
>                 return type.AllocateObject(document, id) as Element;
>         }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20061006/56aa9f66/attachment.html>

More information about the Ironpython-users mailing list