Hi,
Program DeSer_1 creates five 'aclass' instances into a List<T> collection,
Serializes to file TestSecData.Dat, then Deserializes into a second (similar) list.
This succeeds!
Then program DeSer_2 is run to Deserialize and fails with the error:
________________________________________________________________________
InvalidCastException was unhandled
------------------------------------------------------------------------
Unable to cast object of type 'System.Collections.Generic.List`1
[ConsoleApplication1.Program+aclass]' to type
'System.Collections.Generic.List`1[ConsoleApplication2.Program+aclass]'.
I don't have any idea as to why. Deserialization must depend upon some information
that is present in DeSer_1, but is missing in DeSer_2. But what?
I hope this doesn"t mean that deserialization can only occur within the same program
that serialized it. -:((
Would appreciate any help on this. -Lew
Program DeSer_1:----------------------------------------------------------------
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;
using System.Text;
//1. Creates five 'aclass' instances into a List<aclass>, and serializes the List<aclass> to "TestSecData.dat"
//2. Deserializes file "TestSecData.dat" into a second list.
//3. Success!
namespace ConsoleApplication1
{
class Program
{
[Serializable]
public class aclass
{
public int level;
public string id;
}
public static List<aclass> acc = new List<aclass>();
public static List<aclass> acci = new List<aclass>();
public static string fileName = "
\\New_MI_InOut\\TestSecData.dat";
static void Main(string[] args)
{
aclass ac;
for (int i = 0; i < 5; i++)
{
ac = new aclass();
ac.level = 0;
ac.id = "";
ac.level = i * 4;
ac.id = (i * 12).ToString();
acc.Add(ac);
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Level {0}, ID {1}", acc[i].level, acc[i].id);
}
SaveAsBinaryFormat(acc, fileName);
LoadFromBinaryFile(fileName);
}
//------------------------------------------------------------------------------------------------------
static void SaveAsBinaryFormat(object objGraph, string fileName)
{
BinaryFormatter binFormat = new BinaryFormatter();
using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
binFormat.Serialize(fStream, objGraph);
fStream.Close();
}
}
//------------------------------------------------------------------------------------------------------
static void LoadFromBinaryFile(string fileName)
{
BinaryFormatter binFormat = new BinaryFormatter();
using (Stream fStream = File.OpenRead(fileName))
{
acci = (List<aclass>)binFormat.Deserialize(fStream);
fStream.Close();
}
}
}
}
Program DeSer_2:----------------------------------------------------------------
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;
using System.Text;
//Program DeSer_2:
//1. Deserializes file "TestSecData.dat" back into List 'acci'.
//2. Fails on Deserialize.
namespace ConsoleApplication2
{
class Program
{
[Serializable]
public class aclass
{
public int level;
public string id;
}
public static List<aclass> acci = new List<aclass>();
public static string fileName = "
\\New_MI_InOut\\TestSecData.dat";
//------------------------------------------------------------------------------------------------------
static void Main(string[] args)
{
LoadFromBinaryFile(@"TestSecData.dat");
}
//------------------------------------------------------------------------------------------------------
static void LoadFromBinaryFile(string fileName)
{
BinaryFormatter binFormat = new BinaryFormatter();
using (Stream fStream = File.OpenRead(fileName))
{
acci = (List<aclass>)binFormat.Deserialize(fStream);
fStream.Close();
}
}
}
}