简体   繁体   English

C#客户端服务器对象列表转换错误

[英]c# client-server List of objects cast error

I am developing a client server application in C#. 我正在用C#开发客户端服务器应用程序。

I am sending from my server to client an List of Students objects which I serialize on the server side and deserialize on client side. 我正在从服务器向客户端发送“ Students List ”对象,这些对象在服务器端进行序列化,并在客户端进行反序列化。 The serialization and deserialization are as follows : 序列化和反序列化如下:

// server side serialization.
List<Student> ordByMedGen = repo.ordByMedGen();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(writer.BaseStream, ordByMedGen);

// client side deserialization.
BinaryFormatter bin = new BinaryFormatter();
List<Student> list = (List<Student>)bin.Deserialize(receive.BaseStream);
Console.WriteLine(list.Count);
// print the list

Using this method I succesfully send a List of Strings from server side and printed on the client side. 使用这种方法,我成功地从服务器端发送了一个字符串List ,并打印在客户端。 When I tried with my Student class I got the following error 当我尝试Student班时,出现以下错误

[A]System.Collections.Generic.List 1[ServerClient.Student] cannot be cast to [B]System.Collections.Generic.List 1[ServerClient.Student]. 1[ServerClient.Student] cannot be cast to [B]System.Collections.Generic.List [A] System.Collections.Generic.List 1[ServerClient.Student] cannot be cast to [B]System.Collections.Generic.List 1 [ServerClient.Student]。 Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\\Windows\\Microsoft.Net\\assembly\\GAC_64\\mscorlib\\v4.0_4.0.0.0__b77a5c561934e089\\mscorlib.dll'. 类型A源自'mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'在位置'C:\\ Windows \\ Microsoft.Net \\ assembly \\ GAC_64 \\ mscorlib \\ v4.0_4.0.0 .0__b77a5c561934e089 \\ mscorlib.dll中”。 Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\\Windows\\Microsoft.Net\\assembly\\GAC_64\\mscorlib\\v4.0_4.0.0.0__b77a5c561934e089\\mscorlib.dll'. 类型B源自位置'C:\\ Windows \\ Microsoft.Net \\ assembly \\ GAC_64 \\ mscorlib \\ v4.0_4.0.0的位置'LoadNether'中的'mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089' .0__b77a5c561934e089 \\ mscorlib.dll中”。

To create the Server.exe and Client.exe I used the following commands: 要创建Server.exe和Client.exe,我使用了以下命令:

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. 我的Student班级也具有[Serialization]属性。 I did searched the web for this kind of errors but i couldn't find anything usefull. 我确实在网上搜索了此类错误,但找不到任何有用的信息。

Later Edit: Maybe at compilation the fact that Student.cs is provided as a reference both on Server and Client somehow confuse the compiler and sees Student as 2 different types as in error: A and B ? 以后的编辑:也许在编译时就提供了Student.cs作为服务器和客户端上的参考,这使编译器有些困惑,并把Student视为错误的两种不同类型:A和B?

Later Edit(2): This problem can be also solved by creating at compilation an dll for Student.cs and referenced on server and client, so the compiler doesen't get confused. 以后的Edit(2):此问题也可以通过在编译时为Student.cs创建一个dll并在服务器和客户端上引用来解决,因此不会使编译器感到困惑。

It sounds like you have two different assemblies - one at the client, one on the server - contributing this type. 听起来您有两个不同的程序集-一个在客户端,一个在服务器上-贡献了这种类型。 Types are defined by their assembly, and even if identical: are not interchangeable. 类型是由它们的组装定义的,即使它们相同:也不能互换。 Basically, BinaryFormatter will fight you every step of the way here. 基本上,BinaryFormatter将在这里与您抗衡。 My advice: don't use BinaryFormatter. 我的建议:不要使用BinaryFormatter。 Most other serializers (XmlSerializer, json.jet, jil, DataContractSerializer, protobuf-net etc) will work perfectly fine in your scenario, and will not complain at all. 大多数其他序列化程序(XmlSerializer,json.jet,jil,DataContractSerializer,protobuf-net等)在您的方案中都可以很好地工作,并且完全不会抱怨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM