Start New Discussion within our Software Development Community

I have a program that takes an object, turns it into an array of bytes and then sends it to another program using sockets (a la http://www.java2s.com/Tutorial/CSharp/0580__Network/Catalog0580__Network.htm Socket Client & Socket Server GUI). My problem is that when the object arrives at the second program, I cannot turn the byte[] back into an object because it recognises it as Server.Object, not Client.Object.
The code I am using to convert it to byte array and back is:

// Convert an object to a byte array
private byte[] ObjectToByteArray(Object obj)
{
    if(obj == null)
        return null;
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    bf.Serialize(ms, obj);
    return ms.ToArray();
}
// Convert a byte array to an Object
private 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;
}

Here is the error message:
http://i90.photobucket.com/albums/k279/rickyoswaldiow/error.jpg

I read somwhere about sharing the class as a resource before I had this issue, but I've lost the site and cannot find it.

Does anyone know how I can either solve this problem, or maybe know another method of sending an object between two apps?

The solution I have discovered is to make a Class Library project, add the class to the new project and build it. This will create a dll file, which can then be added to "References" in the solution explorer. Finally, add the name of the Class Library to the using statements and you will be able to make objects from the classes defined in the Class Library.

Don't forget to remove the class from the original projects, and make sure that the class is public!

This article has been dead for over six months. Start a new discussion instead.