[IronPython] Implementing an interface
Neil Hodgson
nyamatongwe at gmail.com
Fri May 20 14:15:46 CEST 2005
I have an interface defined in C# that I would like to implement in
IronPython 0.7.5. Similar code works in Jython. The interface looks
like:
public interface IDocumentChangeListener {
void StateAltered();
void InvalidateAll();
void InvalidateRange(int a, int b);
};
Trying to implement the interface in IronPython like this fails:
class IDCLForward(IDocumentChangeListener):
def __init__(self, form):
self.form = form
def StateAltered(self):
print "IDCL altered"
self.form.StateAltered()
# ...
with a trace:
System.ArgumentException: Parent type was not extensible by the given type.
at System.Reflection.Emit.TypeBuilder.SetParent(Type parent)
at IronPython.AST.NewTypeMaker.CreateNewType()
at IronPython.AST.NewTypeMaker.GetNewType(String mod, String name,
Tuple bases, IDictionary`2 dict)
at IronPython.Objects.UserType..ctor(String mod, String name, Tuple
bases, IDictionary`2 dict)
at IronPython.Objects.Ops.MakeClass(String mod, String name, Tuple
bases, IDictionary`2 vars)
at __main__.Initialize() in
C:\os\sinkworld\tentacle\csharp\TentacleP.py:line 24
...
========================================
Sliding in a concrete C# class between the interface and IronPython
class helps avoid stack traces but does not lead to the IronPython
class being called.
public class IDCL : IDocumentChangeListener {
public IDCL() {
}
public virtual void StateAltered() {
Console.Error.WriteLine("StateAltered");
}
public virtual void InvalidateAll() {
Console.Error.WriteLine("InvalidateAll");
}
public virtual void InvalidateRange(int a, int b) {
Console.Error.WriteLine("InvalidateRange");
}
};
class IDCLForward(IDCL):
def __init__(self, form):
self.form = form
========================================
Jython likes to have the interface __init__ed but this also upsets
IronPython.
class IDCLForward(IDCL):
def __init__(self, form):
IDCL.__init__(self)
self.form = form
IronPython.Objects.PythonAttributeError: type object 'SinkWorld.IDCL'
has no attribute '__init__'
at IronPython.Objects.Ops.GetAttr(Object o, String name)
at __main__.__init__$f0(Object self, Object form) in
C:\os\sinkworld\tentacle\csharp\TentacleP.py:line 26
========================================
What should I do to make this work?
Neil
More information about the Ironpython-users
mailing list