Serialization
-
I am having a problem with serialization and deserialization of classes using the BinaryFormatter. I have two different programs and I want to serialize a class in the Test1 program and transfer it using sockets to the Test2 program and deserialize it into a replica class. When I attempt to deserialize in the Test2 program, I get the following System.IO.FileNotFoundException: File or assembly name Test1, or one of its dependencies, was not found. Here is an example of how I am serializing: SomeClass sc = new SomeClass(); sc.var1 = 12; sc.var2 = m_strTemp; BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms,(object)sc); After receiving buffer of serialized data, here is an example of how I am deserializing: BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(buffer,0,nBytesRead,true,true); SomeClass sc = (SomeClass)bf.Deserialize(ms); What am I doing wrong? How can I deserialize in an entirely different process, in a different program, on a different machine and not receive this error? Thanks! Donald
-
I am having a problem with serialization and deserialization of classes using the BinaryFormatter. I have two different programs and I want to serialize a class in the Test1 program and transfer it using sockets to the Test2 program and deserialize it into a replica class. When I attempt to deserialize in the Test2 program, I get the following System.IO.FileNotFoundException: File or assembly name Test1, or one of its dependencies, was not found. Here is an example of how I am serializing: SomeClass sc = new SomeClass(); sc.var1 = 12; sc.var2 = m_strTemp; BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms,(object)sc); After receiving buffer of serialized data, here is an example of how I am deserializing: BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(buffer,0,nBytesRead,true,true); SomeClass sc = (SomeClass)bf.Deserialize(ms); What am I doing wrong? How can I deserialize in an entirely different process, in a different program, on a different machine and not receive this error? Thanks! Donald
Classes in .NET are assembly specific, so if you and I both create a class called
Foo
with the exact same code but you have yours in the assemblydonald.dll
but mine is in the assemblyjames.dll
they are considered incompatible types if the contents of the two assemblies differ. The way to get around it is to place the classes that will be shared between the two applications into a separate assembly (class library to use VS.NET terms). Then the client program and the server program both reference this assembly. HTH, James "And we are all men; apart from the females." - Colin Davies -
Classes in .NET are assembly specific, so if you and I both create a class called
Foo
with the exact same code but you have yours in the assemblydonald.dll
but mine is in the assemblyjames.dll
they are considered incompatible types if the contents of the two assemblies differ. The way to get around it is to place the classes that will be shared between the two applications into a separate assembly (class library to use VS.NET terms). Then the client program and the server program both reference this assembly. HTH, James "And we are all men; apart from the females." - Colin Davies