writing/reading files

  • Thread starter Thread starter Joan
  • Start date Start date
J

Joan

Hello,
Let's say I have a class A, and classes B and C that inherit from A. I want
to save the content of B and C into a file and read it afterwards.
Is there a way to write streams of a specific 'type'? I mean, create a
StreamWriter of 'class A' type.

thanks!
 
Joan said:
Let's say I have a class A, and classes B and C
that inherit from A. I want to save the content of
B and C into a file and read it afterwards. Is
there a way to write streams of a specific 'type'?
I mean, create a StreamWriter of 'class A' type.

Unless the inheritance (being able to load a B object as though it were an A
object) is particularly important to you, XmlSerializer might be
appropriate.

P.
 
mmm... A is abstract, I want to access the properties of both B and C...
And I don´t like the xml solution, I'd like to have the file in binary
 
Sounds like you need to visit the BinaryFormatter, which produces a compact
byte stream suitable for writing instances of classes to binary files, and
being able to read them back and deserialize them back into live class
instances.
 
Yep, that's it. I finally found it, but I have still some questions about
it...
I have to mark [Serializable] each A, B and C classes. Can I serialize, for
example, n B objects and m C objects in one single file and the deserialize
them automatically? or do I have to write a kind of 'header' to the file to
know when each B and C object begins and ends?
 
Back
Top