Binary Serialization over the net

G

Guest

Hello

I need to send data (a whole class) from a Sever to a Client.
I convert this class to a binary array using Binary Serialization:

public byte[] ToBytes(){
try{
IFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream,this);
byte[] Bytes = new byte[stream.Length];
stream.Seek(0,System.IO.SeekOrigin.Begin);
stream.Read(Bytes,0,(int)stream.Length);
return Bytes;
}catch{
return null;
}
}

On the client side I recive the array of bytes and Deserialize them using:

public static CQuery FromBytes(byte[] Bytes){
try{
IFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream(Bytes.Length);
stream.Write(Bytes,0,(int)Bytes.Length);
stream.Seek(0,System.IO.SeekOrigin.Begin);
return (CQuery)formatter.Deserialize(stream);
}
catch{
return null;
}
}

My problem is that the client needs to know the Class type (casting after
deserialize) and the only way to do this is have the *same* DLL contating the
class on both server & client.

How can I do Serialization without having to supply the DLL but rather
define the class (exactly the same mathods) in the same way it is defined on
the server side.

Gilad.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


The client NEEDS the dll no matter what, in the DLL is where the code of
the class is declared. Otherwise how the client could invoke methods or
properties of the instance?

Just for sake of clarity, remember that you send an INSTANCE of a class, not
a class (AKA type)

cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top