OptionalField attribute doesn't seem to work

R

rich.quackenbush

I was very excited when version 2.0 of the framework came out... it
looked as though the OptionalField atribute would help me sort out my
serialization woes. So far, it hasn't through.

All I'm trying to do is add fields to objects which have already been
serialized. For example, I have serialized this object (and
deserialized successfully):

[Serializable()]
class Person
{
private string _FirstName = "";
private string _LastName = "";

... Public Properties omitted
}

I then added a field marked with the OptionalField attribute

[Serializable()]
class Person
{
private string _FirstName = "";
private string _LastName = "";

[System.Runtime.Serialization.OptionalField]
private string _Title = "";

... Public Properties omitted
}

I then got the old "Wrong number of Members. Object SqlXMLTest.Person
has 5 members, number of members deserialized is 4." exception.

I thought that the OptionalField attribute was designed to solve this
exact problem. Has anyone seen this?

BTW - I'm using the SOAPFormatter.

Thanks,
-Rich
 
R

rich.quackenbush

Sorry about that... You are absolutely right.

I've also gotten closer to the issue... This issue appears when trying
to desserialize an object with a Guid field AND an OptionalField.

The below code should work by pasting over the class creating when
making a Console Application.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;

namespace ConsoleApplication1
{

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;

[Serializable()]
class Person
{

//Changing this to a string gets rid of the error
private Guid _PersonID;

private string _FirstName = "";
private string _LastName = "";

//Save the object, then uncomment these two lines and
//[OptionalField]
//private string _Title = "";

public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}

public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}

public void Save()
{

System.IO.FileStream oStream = new
System.IO.FileStream("c:\\Person.xml",
System.IO.FileMode.Create);

IFormatter oFormatter = (IFormatter)new SoapFormatter();

oFormatter.Serialize(oStream, this);

oStream.Close();

}

public static Person LoadPerson()
{

System.IO.FileStream oStream = new
System.IO.FileStream("c:\\Person.xml",
System.IO.FileMode.Open);

IFormatter oFormatter = (IFormatter)new SoapFormatter();

//After adding a field (e.g. _Title) and there is a Guid
field,
//the serialization exception occurs here
Person oPerson = (Person)oFormatter.Deserialize(oStream);

oStream.Close();

return oPerson;

}

}


class Program
{
static void Main(string[] args)
{

Console.WriteLine("Press S to save a person, L to load a
person");
ConsoleKeyInfo oKeyInfo = Console.ReadKey();

if (oKeyInfo.Key == ConsoleKey.S)
{
Person oPerson = new Person();

oPerson.FirstName = "Kevin";
oPerson.LastName = "Jamison";

oPerson.Save();

Console.WriteLine("Person saved.. Press any key to
exit");
Console.ReadKey();
}
else if (oKeyInfo.Key == ConsoleKey.L)
{
Person oPerson = Person.LoadPerson();

Console.WriteLine("Person loaded \"{0}\"... \nPress any
key to exit",
oPerson.FirstName);
Console.ReadKey();
}
else
{
Console.WriteLine("Invalid key.. Press any key to
exit");
Console.ReadKey();
}
}
}
}


Why would the presence of a Guid type make a difference in
versionability.
 
J

Jon Skeet [C# MVP]

Sorry about that... You are absolutely right.

I've also gotten closer to the issue... This issue appears when trying
to desserialize an object with a Guid field AND an OptionalField.

<snip>

Right. Thanks for the code. It fails for me too. Interestingly, it
works with the binary formatter.

I suggest you report it as a bug (with the complete code below) at
http://lab.msdn.microsoft.com/productfeedback/default.aspx

It's worth giving all the information you've found out about when it
fails and when it works.
 

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