[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

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"
	# ...

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() {
	public virtual void InvalidateAll() {
	public virtual void InvalidateRange(int a, int b) {

class IDCLForward(IDCL):
	def __init__(self, form):
		self.form = form


   Jython likes to have the interface __init__ed but this also upsets

class IDCLForward(IDCL):
	def __init__(self, form):
		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?


More information about the Ironpython-users mailing list