Reflection and descendants of Control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I asked this question a while ago, but I now have more information on the
problem.

I have a set of Classes inherited from control that make up a simulation
model. The user drops them on a MDI form to create the model.

Because Control is not serializeable, I am using Reflection to pick up the
fields from the base types when the model is saved.

This works well for most of the properties but fails to save the Text
field, if the control has been added to a form.

If you just create the control and serialize it, the process works
perfectly. However as soon as the control has been added to the controls
collection of a form, reflection returns null for the Text field.

Does anybody out there have any ideas (in particular if anybody at Microsoft
has any thoughts) I would be grateful.
 
Hi Tony,

Short answer - try to get the actual type of the control and invoke the Text
property getter on the actual type, not on the base type.

Long answer - you should separate the model data from its view. Design the
model as a set of serializable classes and use the custom controls just to
display the current state of the model. You can search the Web for the
Model-View-Controller design pattern description for more information on how
this should be properly done.
 
Back
Top