.NET Framework Bookmark and Share   
 index > .NET Remoting and Runtime Serialization > Getting a class recognized across two processes
 

Getting a class recognized across two processes

I am using the BinaryFormatter to serialize an object of class X to a file. Then another program reads in the file and deserialize back into the object. If I put the declaration of class X in a separate cs file and compile it to a DLL, include it as a reference in both programs, it works fine. But there is a DLL file needed at runtime for each program.

If I merely include the exactly same class X code in each program, deserialization comes with "Unable to cast object of type 'namespace.X' to type namespace.X'" I suppose this is to be expected as there is no way from the source code to uniquely identify the class.

How can I make deserialization work without having to include a separate DLL file at runtime?

Thanks.
K.Kong
You don't (without a lot of hassle). Part of the Type name is the assembly it came from, so the Type in your two applications is different.

The easiest thing to do (and I'd argue most proper) is to have a shared assembly for your shared Types (as you've already discovered).

The next easiest thing to do is use XML serialization instead. If you are adamant on having your types hosted in two different assemblies, you can accomplish this easier with XML serialization.

Finally, for binary serialization, look into ISerializationSurrogate. Basically you'd have to forego automatic binary serialization in favor of ISerializable, but it's a way to do it.
  • Marked As Answer byK.Kong Monday, September 14, 2009 1:13 AM
  •  
Adam Sills
You don't (without a lot of hassle). Part of the Type name is the assembly it came from, so the Type in your two applications is different.

The easiest thing to do (and I'd argue most proper) is to have a shared assembly for your shared Types (as you've already discovered).

The next easiest thing to do is use XML serialization instead. If you are adamant on having your types hosted in two different assemblies, you can accomplish this easier with XML serialization.

Finally, for binary serialization, look into ISerializationSurrogate. Basically you'd have to forego automatic binary serialization in favor of ISerializable, but it's a way to do it.
  • Marked As Answer byK.Kong Monday, September 14, 2009 1:13 AM
  •  
Adam Sills

You can use google to search for other answers

Custom Search

More Threads

• why is MarshaledObject called twice
• Use .net serialization or a custon soultion?
• Getting problem extracting bytes of an Object using marshalling....
• .NET Serialization traps and flaws
• Remoting difficulties, sending object on server to client (unrequested)
• Threading in Remoting
• .NET Remoting not working on different version of VS 2005 and .NET framework
• Cryptography custom sink
• Remoting with a Singleton newbie question
• is this transaction completing on its own!?