If you're going to use binary serialization, the big problem you'll run into when sending serialized objects to another application is that objects serialized using binary formatter record not only the class name, but the namespace and the assembly name as well. So even if you successfully serialize and then transfer an object via tcp, when you attempt to deserialize it in the remote application, it will fail.
The way to deal with this is to use
SerializationBinder.BindToType.
Using BindToType, you can peek into the object before it is deserialized, and instruct BinaryFormatter to get the target Object Type from current assembly.
I have a TCP library here on codeproject that allows you to send serialized objects between client and server, and also between clients connected to your server. The code is available in the download for the article:
https://www.codeproject.com/Articles/1192106/Abstracting-TCP-Communications-and-adding-what-sho
I would download the library and have a look - it should do what you need. If you're still interested in doing it yourself, here's a look at how I set up BindToType (in c#):
public class TypeConvertor : System.Runtime.Serialization.SerializationBinder
{
private Assembly targetAssembly;
public TypeConvertor(Assembly targetAssembly)
{
this.targetAssembly = targetAssembly;
}
public override Type BindToType(string assemblyName, string typeName)
{
Type returntype = null;
assemblyName = targetAssembly.FullName;
Assembly.Load(assemblyName);
returntype = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName), false, true);
if (returntype == null && typeName.Contains("."))
{
String vbAssemName = Regex.Split(typeName, @"\.")[0] + ".";
typeName = typeName.Replace(vbAssemName, "");
returntype = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName), false, true);
if (returntype == null)
{
try
{
Type[] types = targetAssembly.GetTypes();
if (types.Length > 0)
{
returntype = Type.GetType(String.Format("{0}, {1}", Regex.Split(types[0].FullName, @"\.")[0] + "." + typeName, assemblyName), false, true);
}
}
catch (Exception) { }
}
}
return returntype;
}
}
The code you will want to look at to see an example of serializing and deserializing objects in that project can be found in Core.cs between lines 399 and 465.
- Pete