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

Kevin Gadd kevin.gadd at gmail.com
Tue Sep 26 18:15:06 CEST 2006

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

I have a test script that looks like this:
from Designer.Schema import Element

class TextStr(Element):
    def __new__():
    def __new__(self):
    def __new__(self, a2):
    def __new__(self, a2, a3):
    def __new__(self, a2, a3, a4):
    def __init__():
    def __init__(self):
        System.Windows.Forms.MessageBox.Show ("__init__1")
    def __init__(self, a2):
    def __init__(self, a2, a3):
    def __init__(self, a2, a3, a4):

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 =
info.Name.Replace(".py", ""), true);
                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/20060926/b01c6146/attachment.html>

More information about the Ironpython-users mailing list