开发者

Casting different objects from TcpClient serialized object stream?

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 ty开发者_如何转开发pes, 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.


I am assuming the objects have already deserialized correctly. I would use a big if object is type then... else...

object deserializedObject = Deserialize(....);
if (deserializedObject is string)
    ProcessString ((string)deserializedObject);
else if (deserializedObject is byte[])
    ProcessBytes ((byte[])deserializedObject);
else if (deserializedObject is Uri)
    ProcessUri ((Uri)deserializedObject);
else
    throwOrLog (deserializedObject);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜