The Polymorphism Problem
But what if you want to use this abstract type in another data class like this:Using PropertyMorphableData
You can implement thePropertyMorphableData interface on the abstract class to solve this. This interface adds a morph method that will be used to determine which subclass to use. The morph method receives an array of properties limited to properties tagged by a PropertyForMorph attribute:
Limitations
Since the morph functionality needs to run early within the data construction process, it bypasses the normal flow of constructing data objects so there are a few limitations:Type restrictions
It is only allowed to use properties typed as string, int, or BackedEnum (int or string)
Enum handling
When a property is typed as an enum, the value passed to the morph method will be an enum
Pre-validation execution
It can be that the value of a property within the morph method is null or a different type than expected since it runs before validation