[Python.NET] Subclassing CLR types

Brian Lloyd brian at zope.com
Thu Oct 30 19:55:02 EST 2003

> Hi,
> subclassing CLR types does work as long as you hold a reference to the 
> instance of the subclass in Python. But when you pass a subclassed 
> object to a CLR object and retrieve this object later through a method 
> of the CLR object you don't get an instance of the subclass but an 
> instance of the CLR base class the subclass was derived from:
> <snip good stuff>
> This will not work if you receive some kind of CLR object which was 
> created internally by some CLR method and not through a wrapper. But you 
> will get all object you created through the wrappers back as instances 
> of the correct type.
> If I'm galloping in the wrong direction please give me a short stop 
> signal. If not ... lets figure out the probably missing details.
> Michael

Hi Michael -

The implementation already does most of what you outlined internally 
using GCHandles - the part that's missing is the lookup to recognize 
a previously wrapped instance. I think that will be straightforward 
to add.

I'll turn your test case into a unit test and try to make sure b2 
solves it.

oooo - right before I sent this, I had a thought that I should 
probably bounce off of the list to see if anyone can see any holes 
in it.

Right now, the subclassing is "shallow" in that you get a subclass 
in Python, but on the managed side the object is of whatever class 
you based from.

I'd like for Python classes to be able to implement abstract .NET 
classes & interfaces at some point, and the way I was planning to 
do that is to generate a real managed subclass whenever you subclass 
a managed class. That will allow for Python implementation of virtuals, 
etc., but it also has the nice side effect that the internals can 
always recognize things subclassed in Python w/o big nasty handle 

I can probably implement that basic 'real subclass per Python subclass' 
as easily as the handle mapper, so that might be the way to solve 
this (and take a step to better integration to boot).

Brian Lloyd        brian at zope.com
V.P. Engineering   540.361.1716              
Zope Corporation   http://www.zope.com 

More information about the PythonDotNet mailing list