5

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:

  1. serialize a BO object
  2. transfer it to another PC
  3. deserialize it with correct type
  4. make a consistency check
  5. save to db
  6. 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?

1 Answer 1

5

If you don't know the type in advance, you cannot currently be doing anything in the C# that depends on the type. BinaryFormatter will already be deserializing it with the correct object type, but you code can usually just refer to the object as.... object:

object aObject = aFormatter.Deserialize(stream);

At this point, there are various options available to you:

  • use a common interface / base-class
  • use dynamic (interesting uses of dynamic include calling the most appropriate overload of a method, and switching into a generic method)
  • test the type explicitly with is, as or GetType(), for special-casing

As an example of the middle option:

object aObject = aFormatter.Deserialize(stream);
GenericMagic((dynamic)aObject);
OverloadMagic((dynamic)aObject);

...

void GenericMagic<T>(T obj) // possibly some constraints here too
{
    T x = ... // now we *have* the type, but as `T`;
              // of course, you still can't do many exciting
              // things unless you add constraints
}

// the correct one of these will be chosen at runtime...
void OverloadMagic(Customer cust) {...}
void OverloadMagic(User user) {...}
void OverloadMagic(Order order) {...}

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.

4
  • Yep, what i ment is the following: Can I send an additonal string which contains the name of the type, and then use this string information as type of the object?
    – Offler
    Commented Oct 9, 2012 at 7:39
  • @Offler the object already knows its type if you are using BinaryFormatter. In the case of serializers like XmlSerializer, it will not - Type.GetType(string) is your best option there, but you need to store the .AssemblyQualifiedName, not just the .FullName (otherwise it won't know what assembly to look in). However, you can't make that the variable type; that just gives you a Type to use at runtime; for example Type type = GetAndParseTheType(); var ser = new XmlSerializer(type); var obj = ser.Deserialize(stream); Commented Oct 9, 2012 at 7:51
  • Okay, i will play around a bit to see how it works. I thought i would need something like aObjType = aObj.GetTypeFromName("ThisisMyType"); MethodInfo aMethod = aObjType.GetMethod("persist"); aMethod.Invoke(aObj, new object[0]);
    – Offler
    Commented Oct 9, 2012 at 7:58
  • @Offler the first is Type type = Type.GetType(string); the second is type.GetMethod("Persist"), however, it would probably be easier and more efficient to use an interface, or dynamic, for that. Commented Oct 9, 2012 at 8:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.