I want to transfer BusinessObjects from one PC to another. If I think about a number of around 40 different object types to transfer the use of many contracts for the different objects would seem to be quite some overload for always the same task: “Send Object A to Computer B and save object to DB” (objects all have a persistent method).
As the Objects can have many different types, I only want to to use a generic method to:
- serialize a BO object
- transfer it to another PC
- deserialize it with correct type
- make a consistency check
- save to db
- is my problem.
At the moment I’m thinking about sending the type as eytra information.
Then I want to do something like:
BinaryFormatter aFormatter = new BinaryFormatter();
aFormatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
Object.ParseTypeFromString(aObjektType) aObject = aFormatter.Deserialize(stream) as Object.ParseTypeFromString(aObjektType);
And afterwards just use generic methods from a base object to save object to database, to keep the transfer classes as simple as possible.
Is there a possibility to do something like this? Or am I going in a completly wrong direction and it would be easier to achieve this task with another approach?
If you don’t know the type in advance, you cannot currently be doing anything in the C# that depends on the type.
BinaryFormatterwill already be deserializing it with the correct object type, but you code can usually just refer to the object as….object:At this point, there are various options available to you:
dynamic(interesting uses ofdynamicinclude calling the most appropriate overload of a method, and switching into a generic method)is,asorGetType(), for special-casingAs an example of the middle option:
Frankly, if I’ve had to deserialize (etc) something unknown I usually prefer to stay non-generic, just using
object,GetType(), and maybe some reflection – it is still generic, even if it doesn’t use generics.