int not serializable?

G

Guest

Hi guys

What I am doing:
I'm saving some configuration stuff in an Class called Options. This class
has got some String, bool and an int Member. I Serialize (binary) this object
in a file.

What happens:
If I deserialize the object, all members have got their proper value except
this **** int member! Before serialize this int has got the value 10 and
after derserialize its value is 0. Is int not Serializable? I don't think so:

[Serializable, StructLayout(LayoutKind.Sequential)]
public struct Int32 : IComparable, IFormattable, IConvertible

What am I doing wrong?

Regards Alexander

My Code:
/**derserialization*/
System.IO.Stream s =
System.IO.File.OpenRead(System.Environment.CurrentDirectory+"/trlc.opt");
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
Object o = null;
try
{
o= b.Deserialize(s);
}
catch(System.Runtime.Serialization.SerializationException)
{
....
}
if(o != null && o is Options)
{
instance = (Options)o;
}


/**serialization*/
System.IO.Stream s =
System.IO.File.OpenWrite(System.Environment.CurrentDirectory+"/trlc.opt");
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
try
{
b.Serialize(s, Options.Instance);
}
catch(System.Runtime.Serialization.SerializationException)
{
....
}
s.Flush();
s.Close();
 
J

Joyjit Mukherjee

Hi,

is the access modifier for the int set to public ? only public fields,
members, properties are serialized & deserialized later.

Regards
Joyjit
 
G

Guest

Hi,

yeah that was the point =D nasty little mistake...

Thanks a lot.

Regards Alexander

Joyjit Mukherjee said:
Hi,

is the access modifier for the int set to public ? only public fields,
members, properties are serialized & deserialized later.

Regards
Joyjit

Alexander Wehrli said:
Hi guys

What I am doing:
I'm saving some configuration stuff in an Class called Options. This class
has got some String, bool and an int Member. I Serialize (binary) this object
in a file.

What happens:
If I deserialize the object, all members have got their proper value except
this **** int member! Before serialize this int has got the value 10 and
after derserialize its value is 0. Is int not Serializable? I don't think so:

[Serializable, StructLayout(LayoutKind.Sequential)]
public struct Int32 : IComparable, IFormattable, IConvertible

What am I doing wrong?

Regards Alexander

My Code:
/**derserialization*/
System.IO.Stream s =
System.IO.File.OpenRead(System.Environment.CurrentDirectory+"/trlc.opt");
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
Object o = null;
try
{
o= b.Deserialize(s);
}
catch(System.Runtime.Serialization.SerializationException)
{
...
}
if(o != null && o is Options)
{
instance = (Options)o;
}


/**serialization*/
System.IO.Stream s =
System.IO.File.OpenWrite(System.Environment.CurrentDirectory+"/trlc.opt");
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
try
{
b.Serialize(s, Options.Instance);
}
catch(System.Runtime.Serialization.SerializationException)
{
...
}
s.Flush();
s.Close();
 
N

Nicholas Paldino [.NET/C# MVP]

Alexander,

Are you sure this is the case? For XmlSerialization, this is the case,
but for the Binary and SoapFormatters, by default, classes with the
Serializable attribute applied and run through the formatters have the
complete state saved, not just public members.


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

Alexander Wehrli said:
Hi,

yeah that was the point =D nasty little mistake...

Thanks a lot.

Regards Alexander

Joyjit Mukherjee said:
Hi,

is the access modifier for the int set to public ? only public fields,
members, properties are serialized & deserialized later.

Regards
Joyjit

Alexander Wehrli said:
Hi guys

What I am doing:
I'm saving some configuration stuff in an Class called Options. This
class
has got some String, bool and an int Member. I Serialize (binary) this object
in a file.

What happens:
If I deserialize the object, all members have got their proper value except
this **** int member! Before serialize this int has got the value 10
and
after derserialize its value is 0. Is int not Serializable? I don't
think so:

[Serializable, StructLayout(LayoutKind.Sequential)]
public struct Int32 : IComparable, IFormattable, IConvertible

What am I doing wrong?

Regards Alexander

My Code:
/**derserialization*/
System.IO.Stream s =
System.IO.File.OpenRead(System.Environment.CurrentDirectory+"/trlc.opt");
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
Object o = null;
try
{
o= b.Deserialize(s);
}
catch(System.Runtime.Serialization.SerializationException)
{
...
}
if(o != null && o is Options)
{
instance = (Options)o;
}


/**serialization*/
System.IO.Stream s =
System.IO.File.OpenWrite(System.Environment.CurrentDirectory+"/trlc.opt");
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
try
{
b.Serialize(s, Options.Instance);
}
catch(System.Runtime.Serialization.SerializationException)
{
...
}
s.Flush();
s.Close();
 

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

Similar Threads


Top