c#client-server类强制转换错误。 [英] c# client-server class cast error.

查看:99
本文介绍了c#client-server类强制转换错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#开发客户端服务器应用程序。



我从服务器向客户端发送一个`Student`类型的对象,我在服务器上序列化在客户端进行反序列化。序列化和反序列化如下:



//服务器端序列化。

学生s1 =新学生(Chuck,Noris );

BinaryFormatter binaryFormatter = new BinaryFormatter();

binaryFormatter.Serialize(writer.BaseStream,s1);



//客户端反序列化。

1.BinaryFormatter bin = new BinaryFormatter();

2.学生s1 =(学生)bin。反序列化(接收。 BaseStream);

3.Console.WriteLine(s1.ToString());

我在运行时收到此错误:

> [A] ServerClient.Student无法强制转换为[B] ServerClient.Student。

>类型A来自'Server,Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null'>上下文'Default'at location> D:\ Faculty \ Workspace\C#\ServerClient\ServerClient\Server.exe。类型B源自> Client,Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null'在上下文'Default'at> location'D:\Faculty\Workspace \C#\ ServerClient \ ServerClient \ Client.exe'。



此外,如果在第2行的客户端我更换:



`学生s1 =(学生)bin.Deserialize(receive.BaseStream);`



with



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

Console.WriteLine(s1.ToString());



everything工作得很好,WriteLine方法是打印toString表示从服务器发送它的对象。



要创建Server.exe和Client.exe,我使用了以下命令:



C:\ Windows \ microsoft.NET \ Framework \v4.0.30319\csc.exe Server.cs Repository.cs Student.cs

C:\ Windows \ Microsoftoft.NET\Framework\v4.0.303 19\csc.exe Client.cs Student.cs

我没有错误或警告。

我的`Student`类也有`[Serialization]`属性。

我在网上搜索过这类错误,但我找不到任何有用的东西





稍后编辑(添加更多代码)。简单地完成我的代码,以便它只是从服务器向客户端发送序列化对象,其中该对象被设置为deserilize,如下所示:



//服务器端发送部分

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

;

;

//where writer is constructed :
StreamReader reader = new StreamReader(this.clientSocket.GetStream()); // clientSocket is the connected client



//客户端接收部分


//client side -receiving part

this.send.AutoFlush = true;

BinaryFormatter bin = new BinaryFormatter();
//Student s = (Student)bin.Deserialize(receive.BaseStream); // this don't work
Object s = bin.Deserialize(receive.BaseStream);  // this works
Console.WriteLine(s.ToString());  // with s of type Object it prints what is supposed
Console.ReadLine();

推荐答案

我不想这么说,但客户端的演员......

I hate to say it, but the cast on client side...
Student s = (Student)bin.Deserialize(receive.BaseStream);



永远不会工作,至少不是这样。< br $> b $ b



为什么?出于安全考虑......如果有人伪造Server.Student并将其发送给您的客户端并使用恶意数据?

如果您的Client.Student密集使用Server.Student数据,那么他们真的可以伤害你的电脑。

这是一个非常好的理由不接受(服务器组装)。学生(客户组装)。学生。

.Net框架不允许这样做。

如果您是* Microsoft,并且您不希望使用您的技术制作的应用程序使其安全性受到损害,您可能会在某处锁定。

这将涵盖开发人员的基础。



想想看......一个项目是由50个开发人员制作的,一个对公司生气并对自己说,'我会告诉他们!'他在家里制作一台服务器。学生因为他知道Client.Student可能会被一些数据炸毁。

那个不允许发生。







更多信息请访问:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/6bcc2f47-d307-4dc1-9c83-ded1d2630e44/cannot-cast-object-of-type- x-to-x?forum = netfxbcl [ ^ ]

似乎有些可以帮助这个的好东西问题,如签名和上面链接中提到的那些。







一个可能解决方法,每当你想在应用程序之间发送数据时,就是在TCP控制的会话下发送简单的数据字符串。

收到后,你可以让一些命令检查该字符串的安全性并将其转换为数据您需要完全控制情况。

或者,像你一样使用通用对象





您也可以尝试,但我无法确定这是否可行,制作具有客户端和服务器功能的单个应用程序,并尝试在2台不同的计算机上安装相同的程序集。 可以工作,但这只是一个想法。





*我不是母语为英语的人,我不知道那句话中哪个是对的:是/是?有人能说出来吗?


will never work, not like that at least.


Why? For security reasons... How about someone forging a "Server.Student" and send that to your Client with malicious data?
If your Client.Student makes intensive use of the Server.Student data, then they can really harm your computer.
That would be a very good reason not to accept ("Server" assembly).Student as ("Client" assembly).Student .
The .Net framework will not allow it.
If you was/were* Microsoft, and you didn't want the app's made with your technology to have their security compromised, you would probably put a lock somewhere.
That´ll cover the developer's bases for him.

Come think of it... A project is made by 50 developers, one gets angry with the company and thinks to himself, 'I´ll show them!' and he makes at home a Server.Student cause he knows Client.Student could be blown up with some data.
That can't be allowed to happen.



There is more information at:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/6bcc2f47-d307-4dc1-9c83-ded1d2630e44/cannot-cast-object-of-type-x-to-x?forum=netfxbcl[^]
There seems to be some goodies that can help this problem, like signatures and things like that which are mentioned in that link above.



One possible workaround, whenever you want to send data between applications is to send simple strings of data under a TCP controlled session.
When received, you can have some commands check that string for security and convert it to the data that you need in full control of the situation.
Or, use a generic Object like you have.


You can also try, but I can't asure this will work, to make a single application that has both Client and Server functionality and try installing the very same assembly in 2 different computers. That could work, but is just an idea.


*I´m not a native English speaker, I don't know which is right in that sentence: was/were? Can someone tell?


这篇关于c#client-server类强制转换错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆