473,769 Members | 3,755 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(o bject sender, System.EventArg s e)
{
Hope y = new Hope();
BinaryFormatter formatter = new BinaryFormatter ();
Stream stream = new FileStream("C:\ \MyFile.bin", FileMode.Create ,
FileAccess.Writ e, FileShare.None) ;
formatter.Seria lize(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(o bject sender, System.EventArg s e)
{
Assembly assm = Assembly.LoadFr om(@"\..\..\..\ CSharp\Csharp.e xe");
Current.Load(as sm.GetName());

BinaryFormatter formatter = new BinaryFormatter ();
Stream stream = new FileStream("C:\ \MyFile.bin", FileMode.Open,
FileAccess.Read , FileShare.Read) ;
Object o = formatter.Deser ialize(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 5022
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.LoadFro m 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**********@y ahoo.com> wrote in message
news:1c******** *************** ***@posting.goo gle.com...
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(o bject sender, System.EventArg s e)
{
Hope y = new Hope();
BinaryFormatter formatter = new BinaryFormatter ();
Stream stream = new FileStream("C:\ \MyFile.bin", FileMode.Create ,
FileAccess.Writ e, FileShare.None) ;
formatter.Seria lize(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(o bject sender, System.EventArg s e)
{
Assembly assm = Assembly.LoadFr om(@"\..\..\..\ CSharp\Csharp.e xe");
Current.Load(as sm.GetName());

BinaryFormatter formatter = new BinaryFormatter ();
Stream stream = new FileStream("C:\ \MyFile.bin", FileMode.Open,
FileAccess.Read , FileShare.Read) ;
Object o = formatter.Deser ialize(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.LoadFr om(args[1]); // second
param - assembly containing the class def
AppDomain.Curre ntDomain.Load(a sm.FullName);
object obj = bFormatter.Dese rialize(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.c om> wrote in message
news:et******** ******@TK2MSFTN GP10.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.LoadFr om(args[1]); // second
param - assembly containing the class def
AppDomain.Curre ntDomain.Load(a sm.FullName);
object obj = bFormatter.Dese rialize(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.c om> wrote in message
news:et******** ******@TK2MSFTN GP10.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
1856
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 causes ie problems. But when deserializing the xml into the object structure, the following exception is beeing thrown: There is an error in XML document (3, 701).
4
7514
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 the data but I can't figure out where the data is in the resulting array. Most of the relevant code is below. When I run the code to desrialize I get no errors but if I try to look at some of the data via the command window I get errors such as...
2
2179
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 instance to a reference type (but not System.String). The commented out code are lines that when enabled (and any conflicting lines disabled) stop the exception from being thrown. Thanks kindly, Andrew.
2
3098
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 format. I can, of course, serialize a class to a file, either binary or XML, but this is not what I am looking for. Currently I am using ToString() or Convert.xxx to do this, but thought that if there was a true serializer, deserializer, that would be...
1
1446
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 end up doing is just deserializing it back into a new instance of the same class. Considering what I just said what I often end up doing is not validating my XML to be deserialized and just try deserializing it into an instance of the class and...
6
1583
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 to disk I get an exception error indicating that the class data members no longer match. To avoid the exception errors, how can I retrieve data from the disk and populate the existing fields found within the file and set defaults for the new...
9
2072
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 if I try to deserialize with another different app, I get an exception (the assembly RegionViewer is the app that serialized the
0
7817
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 service, and due to which my workflow gets stalled. When I searched the net, I found many posts reporting this error, but none with a solution. There is no code involved in the invocation, everything is handled by Adobe tool itself. I just specify the...
1
11816
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 XmlNodeList. I am trying to deserialize the nodes using a foreach as follows: foreach(XmlNode lineItem in LineItemsNodeList) An abbreviated example of the nested lineItem node looks like this:
0
9583
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3955
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3560
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.