Program DeSer_1 creates five 'aclass' instances into a List
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
//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
public static List
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
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
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
fStream.Close();
}
}
}
}
Jean PaulPosted Dec 2, 2010, 2:56 AM
For adding project reference, I have quickly created a blog.
http://jeanpaulva.wordpress.com/2010/12/02/adding-references-in-visual-studio/
LewisPosted Dec 1, 2010, 2:46 PM
Thanks very much for your extremely helpful reply. It all works now, although I am not at all sure how to make that reference to ConsoleApplication37 project in ConsoleApplication1 project.
Do I use the Browse tab, and if so, what then?
- Lew
Jean PaulPosted Dec 1, 2010, 11:01 AM
Some of the remark I have to give is:
- You are defining aclass in both the applications (that is redundant)
- You can use conventions like AClass, List
Coming back to the solution, I was able to run it after doing the fixes, the fixes fall into:
1) Removing the second aclass definition
2) Making the first application Program class public
3) Adding project reference to the first application from second
4) Modifying the second application with Program.aclass reference
You can check the attached source file..
Mark as answer if it solves, else let me know.