I am serializing an object and storing it on a file using one assembly and reading it and deserializing it using a different assembly. When the second assembly attempts to deserialize the object I get back the following error: Unable to find assembly 'CreateData, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
Googling around I found that in order to solve my problem I have to bind the deserializer to the new assembly, the one that does the reading; however when I try to implement the code I found, I get back exactly the same message.
I found that in the ReadData program when the BindChanger method executes, typeTodeserialize returns null and I can’t figure out why. Can someone point me in the right direction? What am I doing wrong in BindChanger that cause it to return null?
Thanks,
Enrique
Here is the code that reads the file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;
namespace ReadData
{
class Program
{
static void Main()
{
// Construct the object that will receive the data
DataStructure ds = null;
// Open the data file
FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
// Construct the binary formatter
BinaryFormatter bf = new BinaryFormatter();
// Change the binding
bf.Binder = new BindChanger();
// deserialize
ds = (DataStructure)bf.Deserialize(fs);
fs.Close();
// Announce success
Console.WriteLine("File Data");
Console.WriteLine("x = {0}, y = {1}", ds.x, ds.y);
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
}
class BindChanger : System.Runtime.Serialization.SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
// Define the new type to bind to
Type typeToDeserialize = null;
// Get the current assembly
string currentAssembly = Assembly.GetExecutingAssembly().FullName;
// Create the new type and return it
typeToDeserialize = Type.GetType(string.Format("{0}, {1}", typeName, currentAssembly));
return typeToDeserialize;
}
}
[Serializable]
class DataStructure
{
public int x;
public int y;
}
}
and this is the code that creates the file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace CreateData
{
class Program
{
static void Main()
{
// Construct and fill the data structure
DataStructure theData = new DataStructure();
theData.x = 123;
theData.y = 456;
// Open a stream for writing
FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
// Construct a binary formatter and
// serialize the data to a stream
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, theData);
fs.Close();
// Announce success
Console.WriteLine("Data file created, press Enter to exit");
Console.ReadLine();
}
}
[Serializable]
class DataStructure
{
public int x;
public int y;
}
}