int not serializable?

  • Thread starter Thread starter Guest
  • Start date Start date
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();
 
Hi,

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

Regards
Joyjit
 
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();
 
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();
 
Back
Top