Let’s assume that we have a Model with several properties on it and we want to create a decorator class of this model to enhance it with some extra properties. Now we want to create a new instance of the DecoratedModel populated with all the property values of Model, perhaps using a constructor with Model as a parameter:
public class DecoratedModel : Model { public DecoratedModel(Model baseModel) { // Populate decorated model generically from baseModel } }
What would be the most generic, concise way to populate the DecoratedModel from the Model?
Well, you should be able to use reflection to reflect over the base classes properties, and then use that to set the child classes properties. I haven’t tested this extensively, but something like this might work: