Fixed writing of polymorphic classes.
Fixes #1903 and probably many others.