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);

and i get this error at runtime:

[A]ServerClient.Student cannot be cast to [B]ServerClient.Student.
Type A originates from 'Server, Version=, Culture=neutral, PublicKeyToken=null' in >the context 'Default' at location >D:\Faculty\Workspace\C#\ServerClient\ServerClient\Server.exe'. Type B originates from >Client, Version=, 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);


Object s1 = bin.Deserialize(receive.BaseStream);

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.

Create methods to serialize and deserialize in the Student class itself. Create an assembly for this class and share it between the Server and Client class.

It appears that the system thinks they are two different classes since they are in two different assemblies.

As Momerath suggested i created two methods in my Student class for serialization and deserialization and an assembly for my Student calss.
Works good now. Thank you!