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 :
// server side serialization.
Student s1 = new Student("Chuck","Noris");
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(writer.BaseStream, s1);
// client side deserialization.
1.BinaryFormatter bin = new BinaryFormatter();
2.Student s1 = (Student)bin.Deserialize(receive.BaseStream);
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:
`Student s1 = (Student)bin.Deserialize(receive.BaseStream);`
with
Object s1 = bin.Deserialize(receive.BaseStream);
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:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe Server.cs Repository.cs Student.cs
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
Later Edit(adding more code).After simplyfing my code so that it just send an serialize object from server to client where that object is deserilizeed, looks as follows:
//server side -sending part
Student s1 = new Student(23, "alex", "dimofte", "Sergent Hategan", "23.04.2013");
List> note = new List>();
Tuple n1 = new Tuple("Analiza", 8);
Tuple n2 = new Tuple("Algebra", 4);
Tuple n3 = new Tuple("Romana", 9);
Tuple n4 = new Tuple("Sport", 8);
Tuple n5 = new Tuple("Analiza", 8);
Tuple n6 = new Tuple("Algebra", 4);
note.Add(n1);
note.Add(n2);
note.Add(n3);
note.Add(n4);
note.Add(n5);
note.Add(n6);
s1.Grades = note;
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(writer.BaseStream, s1);
writer.AutoFlush = true
;
StreamReader reader = new StreamReader(this.clientSocket.GetStream());
//client side -receiving part
this.send.AutoFlush = true;
BinaryFormatter bin = new BinaryFormatter();
Object s = bin.Deserialize(receive.BaseStream);
Console.WriteLine(s.ToString());
Console.ReadLine();