473,320 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problem in Deserializing...

Well, here's the situation.. It's pretty simple; just that I can't get
it to work.

I have 2 Executables..

The first one is called CSharp.exe which is a simple WinForm App. I
have a single button on the form which does the following OnClick.

// CSharp.cs
private void button1_Click(object sender, System.EventArgs e)
{
Hope y = new Hope();
BinaryFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("C:\\MyFile.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, y);
stream.Close();
}

Within the same file I have the definition of the class "Hope" as
follows:

// Also in CSharp.cs
[Serializable]
public class Hope
{
private int i;
private string s;
public long l;
public double d;

public Hope()
{
i = 1;
s = "Lucas";
l = 123;
d = 3.1415;
}
}
The code above is pretty straightforward and simply serializes an
instance of the "Hope" object to a file C:\MyFile.bin.
The project compiles (to CSharp.exe) and runs fine.

The second executable is also a Winform App called Another.exe.

The Main form again has a single button which does the following:

// Another.cs
private void button1_Click(object sender, System.EventArgs e)
{
Assembly assm = Assembly.LoadFrom(@"\..\..\..\CSharp\Csharp.exe");
Current.Load(assm.GetName());

BinaryFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("C:\\MyFile.bin", FileMode.Open,
FileAccess.Read, FileShare.Read);
Object o = formatter.Deserialize(stream);
stream.Close();
}

Here I'm simply trying to deserialize that "Hope" Object that was
serialized to file. There is no reference to the CSharp.exe (I don't
think that it's even possible). So this program has no idea what the
"Hope" class is..

However, the 'Deserialize' method works fine when the CSharp.exe is in
the same directory as Another.exe. But not if it is elsewhere. (Gives
a "Cannot find the assembly CSharp..." error!)

I tried Loading CSharp.exe into the AppDomain, but it still complains
that it cannot load the "CSharp" assembly when the Deserialize method
is invoked.

I really don't want to have to copy that assembly into the same folder
as Another.exe.

I hope that all that makes some amount of sense...
Any help would be most appreciated..

Thanks,
Lucas.
Nov 15 '05 #1
5 4995
Hi Lucas,
The problem you are having is straight forward too.. :)
You have one class that you want to serialize using one assembly and
deserialize in another assembly, right? The easiest way to do this is to
have one common project that has the class you want to serialize. Reference
this assembly in the other 2 projects. Hope this solves ur issue.
Now the assembly not found error... In the button_Click event u r loading
an assembly using Asembly.LoadFrom api. This api looks for the the assembly
first in the local directory and then in the Global assembly cache. Since it
didnot find the asembly it gave the error.

HTH.. Do tell me if u have any problems
Rak

"Lucas" <lu**********@yahoo.com> wrote in message
news:1c**************************@posting.google.c om...
Well, here's the situation.. It's pretty simple; just that I can't get
it to work.

I have 2 Executables..

The first one is called CSharp.exe which is a simple WinForm App. I
have a single button on the form which does the following OnClick.

// CSharp.cs
private void button1_Click(object sender, System.EventArgs e)
{
Hope y = new Hope();
BinaryFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("C:\\MyFile.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, y);
stream.Close();
}

Within the same file I have the definition of the class "Hope" as
follows:

// Also in CSharp.cs
[Serializable]
public class Hope
{
private int i;
private string s;
public long l;
public double d;

public Hope()
{
i = 1;
s = "Lucas";
l = 123;
d = 3.1415;
}
}
The code above is pretty straightforward and simply serializes an
instance of the "Hope" object to a file C:\MyFile.bin.
The project compiles (to CSharp.exe) and runs fine.

The second executable is also a Winform App called Another.exe.

The Main form again has a single button which does the following:

// Another.cs
private void button1_Click(object sender, System.EventArgs e)
{
Assembly assm = Assembly.LoadFrom(@"\..\..\..\CSharp\Csharp.exe");
Current.Load(assm.GetName());

BinaryFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("C:\\MyFile.bin", FileMode.Open,
FileAccess.Read, FileShare.Read);
Object o = formatter.Deserialize(stream);
stream.Close();
}

Here I'm simply trying to deserialize that "Hope" Object that was
serialized to file. There is no reference to the CSharp.exe (I don't
think that it's even possible). So this program has no idea what the
"Hope" class is..

However, the 'Deserialize' method works fine when the CSharp.exe is in
the same directory as Another.exe. But not if it is elsewhere. (Gives
a "Cannot find the assembly CSharp..." error!)

I tried Loading CSharp.exe into the AppDomain, but it still complains
that it cannot load the "CSharp" assembly when the Deserialize method
is invoked.

I really don't want to have to copy that assembly into the same folder
as Another.exe.

I hope that all that makes some amount of sense...
Any help would be most appreciated..

Thanks,
Lucas.

Nov 15 '05 #2
Thanks for the answer...

The thing is that I want to have a completely generic Application that
is capable of deserializing any object...

If the application is given :
1. The file that contains the serialized object
2. The path to the assembly where that object is defined

It should be able to deserialize it..

Moving the class into a separate assembly and referencing it in both
projects is as good as copying it local.

.NET allows you to instantiate an object that is defined in a remote
dll, without having to copy it local. So I'm sure that this is scenario
is possible as well..

Lucas.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
Thanks for the answer...

The thing is that I want to have a completely generic Application that
is capable of deserializing any object...

If the application is given :
1. The file that contains the serialized object
2. The path to the assembly where that object is defined

It should be able to deserialize it..

Moving the class into a separate assembly and referencing it in both
projects is as good as copying it local.

.NET allows you to instantiate an object that is defined in a remote
dll, without having to copy it local. So I'm sure that this is scenario
is possible as well..

Lucas.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4
Luke,

static void Main(string[] args)
{
BinaryFormatter bFormatter = new BinaryFormatter();
FileStream fStream = File.Open(args[0],FileMode.Open); // first
param - serialized file
Assembly asm = Assembly.LoadFrom(args[1]); // second
param - assembly containing the class def
AppDomain.CurrentDomain.Load(asm.FullName);
object obj = bFormatter.Deserialize(fStream);
}

You can load the assembly into your appdomain and deserialize the
object, fine. But you wont be able to access members of ur class because
that class wont visible at compile time, rt?
Here is one solution..
Create an interface that will generalize the classes ur going to serialize..
The classes u want to serialize will implement the interface..
Serialize this class...
Deserialize and cast it into the interface..
Access the object using interface methods..

Does this solve ur problem?
Rak
"Luke" <lu***@online.com> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
Thanks for the answer...

The thing is that I want to have a completely generic Application that
is capable of deserializing any object...

If the application is given :
1. The file that contains the serialized object
2. The path to the assembly where that object is defined

It should be able to deserialize it..

Moving the class into a separate assembly and referencing it in both
projects is as good as copying it local.

NET allows you to instantiate an object that is defined in a remote
dll, without having to copy it local. So I'm sure that this is scenario
is possible as well..

Lucas.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #5
Luke,

static void Main(string[] args)
{
BinaryFormatter bFormatter = new BinaryFormatter();
FileStream fStream = File.Open(args[0],FileMode.Open); // first
param - serialized file
Assembly asm = Assembly.LoadFrom(args[1]); // second
param - assembly containing the class def
AppDomain.CurrentDomain.Load(asm.FullName);
object obj = bFormatter.Deserialize(fStream);
}

You can load the assembly into your appdomain and deserialize the
object, fine. But you wont be able to access members of ur class because
that class wont visible at compile time, rt?
Here is one solution..
Create an interface that will generalize the classes ur going to serialize..
The classes u want to serialize will implement the interface..
Serialize this class...
Deserialize and cast it into the interface..
Access the object using interface methods..

Does this solve ur problem?
Rak
"Luke" <lu***@online.com> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
Thanks for the answer...

The thing is that I want to have a completely generic Application that
is capable of deserializing any object...

If the application is given :
1. The file that contains the serialized object
2. The path to the assembly where that object is defined

It should be able to deserialize it..

Moving the class into a separate assembly and referencing it in both
projects is as good as copying it local.

NET allows you to instantiate an object that is defined in a remote
dll, without having to copy it local. So I'm sure that this is scenario
is possible as well..

Lucas.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas | last post by:
Hi, I implemented a composite pattern which should be serializable to xml. After spending some time in the newsgroups, i finally managed serializing, even with utf-8 instead of utf-16, which...
4
by: Wayne Wengert | last post by:
Using VB.NET I want to read in an XML file that has an array of objects and then step through the resulting array in code. I build a class to define the structure and I am running code to read in...
2
by: Andrew G. J. Fung | last post by:
Can anyone please explain to me why the code below throws an exception? It seems to occur when deserializing an instance of a subclass, whose base class holds a struct, where said struct holds an...
2
by: Earl Teigrob | last post by:
I am saving and restoring value types such as Int32, DateTime and Boolean in strings. I was wondering if there is a mechanism build into .NET for serializing and deserializing these to string...
1
by: Bob Rock | last post by:
Hello, always having to validate an XML stream against a XSD may add up an important overhead. My XMLs are usually the result of serializing a class instance and often in my applications what I...
6
by: Steve Teeples | last post by:
I use serialization to write class data to a file. During my development of this class I need to add properties or fields on occation. After adding a property, when deserializing the data saved...
9
by: norvinl | last post by:
Hi, I'm serializing a class and using shared memory / deserialization to send it to other processes. I can serialize with one app and deserialize with another instance of the same app. But...
0
by: Sivajee Akula | last post by:
Hello All, I am trying to consume a .NET Service from Adobe LiveCycle Workflow. The service deals with complex objects. I am getting the following exception at the time of invocation of the...
1
by: =?Utf-8?B?SmVyZW15X0I=?= | last post by:
I am working on an order entry program and have a question related to deserializing nodes with nested elements. The purchase order contains multiple line items which I select using an...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.