Serialization of an object containing an ArrayList

B

Bry

I have an object which contains an object of type ServiceController[].
When I attempt to serialize this object using a binary formatter, I
receive the runtime error message, "...is not marked as serializable".

Code snippet:

[serializable]
class MyObject
{

// One of the properties
private ServiceController[] serviceState;

// more code

public void LoadServices()
{
serviceState = ServiceController.GetServices();
}
}

How can I easily make this object serializable?

Thanks,
Bry.
 
M

mdb

I have an object which contains an object of type ServiceController[].
When I attempt to serialize this object using a binary formatter, I
receive the runtime error message, "...is not marked as serializable".

You can't really make ServiceController serializable - it represents a
local resource. You should try to keep the classes that you define as
serializable to a minimum memory footprint, and only store the values that
you actually need to serialize.
 
B

Bry

I'm attempting to preserve this data between sessions of my application
on the same computer. e.g. The data is saved as the application quits,
but is reloaded when the application is restarted.

As I don't require all of the properties stored in each
ServiceController instance (I only need to preserve Name and State), I
supose I should create a class of my own, containing only the required
information.

Many Thanks.
 
M

mdb

As I don't require all of the properties stored in each
ServiceController instance (I only need to preserve Name and State), I
supose I should create a class of my own, containing only the required
information.

Sounds like you are on the right track... to extend it though, I would say
you should only preserve the Name (not state). The state should be queried
at the time that you need it, since between sessions of your application,
the state may have changed.
 
B

Bry

Just for information, I need to preserve the previous state (for
comparison), so it should work fine.

Thanks for your help.
Bry.
 

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

Top