Congratulations - C# Corner Q4, 2022 MVPs Announced
Why Join
Become a member
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
C# Corner Home
Technologies
Monthly Leaders
ASK A QUESTION
Forum guidelines
Lewis
0
10
0
Binary Deserialization of a file Serialized in Another Program
Nov 30 2010 8:32 PM
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();
}
}
}
}
Reply
Answers (
3
)
How to import excel and foxpro (.dbf) files into Datagrid using c #?
Passing parameters to a eventhandler?