简体   繁体   中英

Convert an object to a byte array in C#, send it over a socket, then convert back into object

So I have a server and a client that communicate various data back and fourth. Initially I had a complicated method that went through the byte array and converted all of its variables and strings, one by one, into what they were supposed to be. I learned I could put all of the variables into an object and convert it to a byte array using

private static byte[] ObjectToByteArray2(Object obj)
    {
        if (obj == null)
            return null;
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream ms = new MemoryStream();
        bf.Serialize(ms, obj);
        return ms.ToArray();
    }

And convert it back using

private static Object ByteArrayToObject(byte[] arrBytes)
    {
        MemoryStream memStream = new MemoryStream();
        BinaryFormatter binForm = new BinaryFormatter();
        memStream.Write(arrBytes, 0, arrBytes.Length);
        memStream.Seek(0, SeekOrigin.Begin);
        Object obj = (Object)binForm.Deserialize(memStream);
        return obj;
    }

The problem is, once I send this byte array across the network to another application, I can't just use this method to convert it back, I get the error "Unable to find assembly 'test1s, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'." test1s is just the name of the little server program I made to play with this. Obviously the application needs some extra information to convert this array back into an object, so is there any way I can do this, or am I going about the problem wrong?

What I want to accomplish here is have an object of nothing but several variables and strings, convert it to a byte array, send it to another application, and convert it back into the object. This way I don't have to play with the byte array to extract all of my variables and strings.

Thanks

There are a myriad of pre-rolled serialization libraries that will help here. BinaryFormatter has some (IMO) undesirable features here - in particular it will only work with the exact same (well, pretty much) dll at both ends.

XmlSerializer, DataContractSerializer and JavaScriptSerializer are good text-based implementations, and will work fine with a compatible contract at both ends (same properties etc - not necessarily the same type/version).

If you have moderate bandwidth needs, or need better CPU performance I would recommend protobuf-net (caveat: I wrote it) which is a fast binary serializer that may help.

This will work if both sides of the communication channel have a reference to exactly the same assembly and exactly the same version of that assembly , either referenced from the program somehow or living in the GAC.

If you want a mechanism more tolerant of version mismatches, consider using XMLSerializer instead (but note that addition/removal/changes in fields/properties may result in incorrect behavior if versions don't match up).

If a compact format is required, you might consider looking at Google Protocol Buffers .

If sending a list of strings (or a dict of strings) is what you want that that is no problem, just send a list (or dict) of strings. Your problem comes because you are trying to send a datatype that the other project doesn't recognize. You don't even need to change any of your functions, you just need to change what you send. Alternatively you can reference test1s from the program receiving the data.

Write your own object from/to bytes convertor instead of using BinaryStream should work.

If you insist, move this object to its own assembly, and add it to both sides as a reference. In that way, .NET Framework should be able to de/serialize the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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