.NET 2.0 (RTM) serialization does not thror exception for missing/changed properties!


T

Tuncay Baskan

Hi all,

Serialization behavior of .NET 2.0 (RTM version) is very strange. I
have the following simple code:

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable]
public class SampleClass {
public SampleClass(string name) {
this.m_Name = name;
}

private string m_Name = "";
public string Name {
get { return m_Name; }
set { m_Name = value; }
}
}

class Program {
static void Main(string[] args) {
Read();
}
static void Write() {
Stream str = File.Create("c:\\deneme1.bin");
BinaryFormatter frm = new BinaryFormatter();
frm.Serialize(str, new SampleClass("A String"));
str.Close();
}
static void Read() {
Stream str = File.OpenRead("c:\\deneme1.bin");
BinaryFormatter frm = new BinaryFormatter();
object l=frm.Deserialize(str);
str.Close();
}
}


Later I change the name of the private field 'm_Name' to 'm_Name1' and
even property name to 'Name1' too. And in Main method call the Read()
method.

I get no exception! No error message, no warning, nothing!

When I debug the Read() method after deserializing I see that class
constructed . I tried the exact same code on Visual Studio 2003 with
..NET 1.1 and it throws serialization exception correctly.

This seems very very weird to me. We tried the same code on different
machines in the LAN and I believe it has nothing to do with
installation issues.

Can anyone enlighten me what is going on?

Thanks.
/tb.
 
Ad

Advertisements

L

Lloyd Dupont

When I debug the Read() method after deserializing I see that class
constructed . I tried the exact same code on Visual Studio 2003 with
.NET 1.1 and it throws serialization exception correctly.

This seems very very weird to me. We tried the same code on different
machines in the LAN and I believe it has nothing to do with
installation issues.

Can anyone enlighten me what is going on?

This is an improvment!
Strict serialization was causing way more problem than it solved.

Anyway if you like an exception to be thrown, .. uh....
I don't know.... there is no obvious way but I think there is a way, I just
can't remember it now..
 
Ad

Advertisements

L

Lloyd Dupont

uh....
I read about it (and also people complaining about that) a long while ago.
mostly when I was interested in Remoting and many remoting users complained
that this exact version matching was real pain for them.

Now, I can't remember where, but maybe you could google for something as
"Serialization C# Remoting missing field 2.0" ?
 

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