TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
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
Forums
Monthly Leaders
Forum guidelines
Lewis
NA
10
0
BinaryFormatter Deserialize result lost when method exits
Nov 18 2010 3:55 PM
Hi, the title says it all. I'm baffled. When I use the three (deserialize) stmts in-line, it all works. But when the stmts are in a specialized method, I lose the deserialized result. The code:
using
System;
using
System.Collections.Generic;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Data;
using
System.Runtime.Serialization.Formatters.Binary;
using
System.IO;
using
System.Windows.Documents;
using
System.Windows.Input;
namespace
WpfApplication1
{
///
<summary>
///
Interaction logic for MainWindow.xaml
///
</summary>
public
partial
class
MainWindow
: Window
{
[
Serializable
]
public
class
aclass
{
public
int
level;
public
string
id;
}
public
List
<aclass> acc =
new
List
<aclass>();
public
List
<aclass> acci =
new
List
<aclass>();
//test
public
MainWindow()
{
InitializeComponent();
}
private
void
button1_Click(
object
sender, RoutedEventArgs e)
{
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,
"TestSecData.dat"
);
LoadFromBinaryFile(
"TestSecData.dat"
);
//works properly if I put the 3 deserialize logic stmts here (instead of calling "LoadFromBinaryFile"):
//BinaryFormatter binFormat = new BinaryFormatter();
//Stream fStream = File.OpenRead("TestSecData.dat");
//List<aclass> acci = (List<aclass>)binFormat.Deserialize(fStream);
}
//------------------------------------------------------------------------------------------------------
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);
}
}
//------------------------------------------------------------------------------------------------------
static
void
LoadFromBinaryFile(
string
fileName)
{
BinaryFormatter
binFormat =
new
BinaryFormatter
();
using
(
Stream
fStream =
File
.OpenRead(fileName))
{
List
<aclass> acci = (
List
<aclass>)binFormat.Deserialize(fStream);
}
//if I stop here and examine 'acci', it shows that the deserialization worked, and 'acci' is loaded
// (with count of 5 'aclass' items)
}
//but.... 'acci' is cleared here when the method is exited! WHY?
}
}
Would appreciate any help. -Lew
Reply
Answers (
1
)
Limit filter descriptor in BDC model using vs 2010
smartphone output reversal (password generator)