问题
I am currently trying to send a serialized object over a TCP connection as follows -
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(clientStream, (Object)Assembly.LoadFrom("test.dll"));
where clientStream is
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
This is the sending part. But can anyone tell me how do I receive this on the client side (i.e. deserialize it on the other end)?
回答1:
Don't serialize the assembly. Send the assembly itself just by loading it as a file and sending those bytes to the other side.
Then, when both sides have the same code, send the object via serialization. I believe the AppDomain which deserializes the object will have to have the relevant assembly loaded (or at least available to be loaded).
回答2:
You are trying to pass an in-memory representation of the assembly over the wire, not the bytes comprising the assembly file itself. Is that really what you want to do?
回答3:
Based on the comments, the answer is completely different.
Instead of using a BinaryFormatter, you should get the location of the assembly through the Location property and then use a FileStream to read the bytes of the assembly and send that over the wire.
Serializing the assembly does nothing more than send over the assembly name. You need to send the entire content of the assembly.
回答4:
Try using BinaryWriter for writing to stream and BinaryReader for reading from it.
来源:https://stackoverflow.com/questions/674194/transferring-assembly-over-tcp