473,382 Members | 1,367 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,382 software developers and data experts.

c# client-server class cast error.

I am developing a client server application in C#.

I am sending from my server to client an object of `Student` type which I serialize on the server side and deserialize on client side. The serialization and deserialization are as follows :

Expand|Select|Wrap|Line Numbers
  1. // server side serialization.
  2.     Student s1 = new Student("Chuck","Noris");
  3.     BinaryFormatter binaryFormatter = new BinaryFormatter();
  4.     binaryFormatter.Serialize(writer.BaseStream, s1);
  5.  
  6.     // client side deserialization.
  7.     1.BinaryFormatter bin = new BinaryFormatter();
  8.     2.Student s1 = (Student)bin.Deserialize(receive.BaseStream);
  9.     3.Console.WriteLine(s1.ToString());
and i get this error at runtime:
>[A]ServerClient.Student cannot be cast to [b]ServerClient.Student.
>Type A originates from 'Server, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in >the context 'Default' at location >D:\Faculty\Workspace\C#\ServerClient\ServerClient \Server.exe'. Type B originates from >Client, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at >location 'D:\Faculty\Workspace\C#\ServerClient\ServerClient \Client.exe'.

Also, if on the client side on line 2 I replace:

Expand|Select|Wrap|Line Numbers
  1. `Student s1 = (Student)bin.Deserialize(receive.BaseStream);`
  2.  
with
Expand|Select|Wrap|Line Numbers
  1.     Object s1 = bin.Deserialize(receive.BaseStream);
  2.     Console.WriteLine(s1.ToString());
everything works just fine, and the WriteLine method is printing toString represetation of object send it from server.

To create the Server.exe and Client.exe I used the following commands:

Expand|Select|Wrap|Line Numbers
  1. C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe Server.cs Repository.cs Student.cs
  2.     C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe Client.cs Student.cs
and I get no errors or warning.
Also my `Student` class have the `[Serialization]` attribute.
I did searched the web for this kind of errors but i couldn't find anything usefull.
Apr 5 '14 #1
0 1327

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Martin | last post by:
using VS2005 - VB.Net I noticed unexpected behaviour when using a combobox which has it's datasource set to a datatable. Also the Valuemember and Displaymember are being set. I'm getting a cast...
1
by: bashanmu | last post by:
i am experiencing the class cast exception for the following code Long index = new Long(-1); . . . . index=(Long)record.getAttribute(some constant name); Explanation: i have a text field...
0
by: buccsailor | last post by:
Hell there, I tried to post a reply to a message thread created back in July 2006 regarding the override of WebRequest, Closed Conenctions and setting KeepAlive to false,but it's been over 60...
2
by: meru | last post by:
The following is a piece of code to calla stored procedure in oracle from java ArrayList arrList = new ArrayList(); arrList.add(new Integer("10")); arrList.add(new Integer("7")); ...
1
by: Angus | last post by:
Why moving a function from protected to public area in class causes error C2556 - overloaded function differs only by return type I have a protected function called GetState like this: enum...
1
by: Rahul | last post by:
I am getting following error: 1) For a xml file "Request.xml" we created a schema "Request.xsd". 2) With the help of xsd.exe we got the C# file Request.cs. 3) We tried to send the object of...
3
by: rahul9885 | last post by:
what is class cast exception? why it comes. I am getting this exception when i am running my application. class files are getting compiled sucessfully. Please help.
4
by: shanu | last post by:
Hi, In my custom classloader i read the bytes using fileinputstream and constructed a class object using define class method. then I have used newInstance method on that loaded class object....
2
by: isaacsanya01 | last post by:
Hello, I have a parent class called UserPreference. This class is composed of 2 child classes namely HighLevelUserPreference and SpecificUserPreference. Please, could you tell me whats wrong with...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.