There are a few ways to do this, depending on how much control you want.
The easiest is to use the [Serializable] attribute. For example,
[Serializable]
public class Ubiquity
{
private Foo f;
public Ubiquity(Foo F)
{
this.f = f;
}
// some other stuff
}
[Serializable]
public class Foo
{
private int a;
public Foo(int A)
{
this.a = A;
}
// class stuff
}
now, when you want to read/write instances of this class:
using System;
using System.IO;
using System.Runtime. Serialization.F ormatters.Binar y;
public class Testing
{
public static void Main(string[] args)
{
Ubiquity u = new Ubiquity(new Foo(100));
// write u to a binary file
// copied from
"ms-help://MS.VSCC.2003/MS.MSDNQTR.2003 APR.1033/cpguide/html/cpconbasicseria lization.htm"
IFormatter formatter = new BinaryFormatter ();
Stream stream = new FileStream("MyF ile.bin", FileMode.Create ,
FileAccess.Writ e, FileShare.None) ;
formatter.Seria lize(stream, obj);
stream.Close();
// read the object back from the file
stream = new FileStream("MyF ile.bin", FileMode.Open, FileAccess.Read ,
FileShare.Read) ;
MyObject obj = (Ubiquity) formatter.Deser ialize(stream);
stream.Close();
}
}
Note that this is an error situation if we don't have [Serializable]
just before class Foo, because Foo will be serialized as well.
See also: ISerializable interfae, SOAP/XML serialization
Durand wrote:
[color=blue]
> How can I write/read a structure into a binary file?
>
> Thanks
>
> Durand
>
>[/color]
In addition to the method in the previous post, you can check the
System.IO.Binar yWriter and System.IO.Binar yReader classes the would provide
better performance and smaller file size than serialization, but require
more work from your side
Best Regards
Sherif
"Durand" <carlos.durand@ terra.com.br> wrote in message
news:#GJvoaLeDH A.3428@tk2msftn gp13.phx.gbl...[color=blue]
> How can I write/read a structure into a binary file?
>
> Thanks
>
> Durand
>
>[/color]
If there are certain members of a class that don't need to be serialized (or
can't), then mark them with the NonSerializable attribute. This is required
on some things like delegates, or framework classes that don't serialize.
Chris
"Ben Taitelbaum" <ben@jbmsystems .com> wrote in message
news:K588b.7698 $ej1.7001@nwrdn y01.gnilink.net ...[color=blue]
> There are a few ways to do this, depending on how much control you want.
> The easiest is to use the [Serializable] attribute. For example,
>
> [Serializable]
> public class Ubiquity
> {
> private Foo f;
> public Ubiquity(Foo F)
> {
> this.f = f;
> }
> // some other stuff
> }
>
> [Serializable]
> public class Foo
> {
> private int a;
> public Foo(int A)
> {
> this.a = A;
> }
> // class stuff
> }
>
> now, when you want to read/write instances of this class:
>
> using System;
> using System.IO;
> using System.Runtime. Serialization.F ormatters.Binar y;
> public class Testing
> {
> public static void Main(string[] args)
> {
> Ubiquity u = new Ubiquity(new Foo(100));
>
> // write u to a binary file
> // copied from
>[/color]
"ms-help://MS.VSCC.2003/MS.MSDNQTR.2003 APR.1033/cpguide/html/cpconbasicseria
lization.htm"[color=blue]
>
> IFormatter formatter = new BinaryFormatter ();
> Stream stream = new FileStream("MyF ile.bin", FileMode.Create ,
> FileAccess.Writ e, FileShare.None) ;
> formatter.Seria lize(stream, obj);
> stream.Close();
>
> // read the object back from the file
> stream = new FileStream("MyF ile.bin", FileMode.Open, FileAccess.Read ,
> FileShare.Read) ;
> MyObject obj = (Ubiquity) formatter.Deser ialize(stream);
> stream.Close();
> }
> }
>
> Note that this is an error situation if we don't have [Serializable]
> just before class Foo, because Foo will be serialized as well.
>
> See also: ISerializable interfae, SOAP/XML serialization
>
> Durand wrote:
>[color=green]
> > How can I write/read a structure into a binary file?
> >
> > Thanks
> >
> > Durand
> >
> >[/color]
>[/color]
Comment