Serialization Exception

  • Thread starter Thread starter Michael Gorbach
  • Start date Start date
M

Michael Gorbach

Hi,
My project is set up with 1 main form and 2 sub forms declared inside
it. Inside the main form i have declared a simulation object (class i
wrote), which is passed in as a parameter to constructors of the 2
subforms.
I am trying to issue a command from the mainform to serialize the
simulation object and save it to a file, using a binaryformatter. For
some reason, im getting a serializationexception which says that both
the main form and the sub forms must all be marked [serializable]. Why
do they need to be marked so when there are no fields of the MainForm
or ViewForm class declared in the simulation object?
 
Michael,

Even though your simulation object doesn't have a field which is of a
form type, it could hold a reference (through another object it holds) to
the form. This is probably the case here. When you serialize an object, it
doesn't just serialize that object, it serializes every object it points to,
as well as every object they point to, and so on, and so on.

Hope this helps.
 
You can prevent the exception by implementing ISerializable instead of using the SerializeAttribute and only adding the data that
should be persisted. It is more work, but this is how the process of serialization accounts for those "special" situations.

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Nicholas Paldino said:
Michael,

Even though your simulation object doesn't have a field which is of a form type, it could hold a reference (through another
object it holds) to the form. This is probably the case here. When you serialize an object, it doesn't just serialize that
object, it serializes every object it points to, as well as every object they point to, and so on, and so on.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael Gorbach said:
Hi,
My project is set up with 1 main form and 2 sub forms declared inside
it. Inside the main form i have declared a simulation object (class i
wrote), which is passed in as a parameter to constructors of the 2
subforms.
I am trying to issue a command from the mainform to serialize the
simulation object and save it to a file, using a binaryformatter. For
some reason, im getting a serializationexception which says that both
the main form and the sub forms must all be marked [serializable]. Why
do they need to be marked so when there are no fields of the MainForm
or ViewForm class declared in the simulation object?
 
this is very strange ... none of the objects references holds a
reference to any form object, yet all the form objects in my program
seem to cause a serializationexception.

I dont understand how either form could possibly have gotten into the
graph of objects to be serialized.
 
here is some code ...
[Serializable]
class Simulation
{
RandGen myGen;
Configuration configuration;
MoveCounter moveCounter;
SimulationSettings settings;
....

class SimulationSettings
{
int n;
double kT, maxDisp, maxDispA, uH, hAngle;
vector boxSize;
....

[Serializable]
class Configuration
{
Nanoparticle[] particles;
vector boxSize;
double pEnergy;
int n;
RandGen myGen = new RandGen();
ArrayList chains = new ArrayList();

[Serializable]
class MoveCounter
{
int p_run_acceptedMoves, p_run_rejectedMoves;
int p_sim_acceptedMoves, p_sim_rejectedMoves;
int sweep_acceptedMoves, sweep_rejectedMoves;
int run_sweeps, sim_runs, sim_sweeps;

the simulation class is the one i am trying to serialize. As you can
see there are no referances to any forms in its field or any fields of
objects it references.
 
i think i understand whats wrong ... its the events in the simulation
class.
There are delegates listening to them that are linked to forms.
How do i fix this problem before serializing the object?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top