从 TcpClient 序列化对象流中转换不同的对象?
我将通过序列化并通过 TcpClient 发送,在服务器/客户端应用程序之间发送不同的对象类型。当我反序列化流时,如何最好地获得正确的类型?
大多数物体的大小将< 100字节,但偶尔也可能达到几十万字节。对于这种情况,将仅传输 5-10 个潜在的类类型。
我想我可以对各种类型进行一系列的 Try-Catch,然后看看什么会成功。尽管我正在考虑在流的开头创建一个固定字段,该字段具有可用于通过 select 语句“手动”识别类型的代码。
请在这里评论什么可能是正确的解决方案。
I'll be sending different object types between server/client applications, by serializing and sending over TcpClient. When I deserialize the stream, how would I best obtain the correct Type?
Most objects size will be < 100 bytes, but occasionally it may be up to a few hundred thousand bytes. For this case it will be only 5-10 potential class types to be transmitted.
I guess I could put a series of Try-Catch for the various types, and see what succeeds. Though I'm thinking about making a fixed field at the start of the stream that has a code which can be used to "manually" identify the type by a select statement.
Please comment on what may be a proper solution here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设对象已经正确反序列化。如果对象是类型,我会使用一个大的...否则...
I am assuming the objects have already deserialized correctly. I would use a big if object is type then... else...