Author Topic: Doubt about derived classes Serialization  (Read 2043 times)

Dechcaudron

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Doubt about derived classes Serialization
« on: June 03, 2014, 06:22:48 PM »
Hi there GCC,

So I was playing around with your code and came up with the following question:
When you send an object that derives from the Continental.player class, which explicitly hides the player's Serialize and Deserialize methods with new ones that don't necessarily write or read the username and password player's properties, we find those variables are still serialized-deserialized. I have tried the same for classes derived from a base IGameCommand that define new Serialize and Deserialize methods, and they behave the same way.

I know you don't have to answer this, but I'm genuinely curious about how you can call a method implemented as new by all the derived classes from the bottom class up to all its base classes. Anyone willing to enlighten me?

Thanks a lot :)
Dechcaudron

AMarinov

  • Global Moderator
  • Newbie
  • *****
  • Posts: 17
    • View Profile
Re: Doubt about derived classes Serialization
« Reply #1 on: June 04, 2014, 09:56:52 AM »
Hi Dechcaudron,

It uses some reflection tricks to achieve this, therefore it does not care if you hide serialization methods in the inheriting class or not, it makes sure all the info is serialized properly.
The idea is that when you inherit a class you would still want to use properties of its base class, otherwise you would just write a new one. What is serialized and what is not is decided in the object implementation of the serialization, depending on the command code it gets, so if you want a specific parts to be sent over network, use a specific command code for the case. Example:
lets say you have a Person class with properties Name and Age, and supporting only one serialization command FULL_INFO=2000
then you have a new class, Student inheriting from Person, and having a single additional property GraduationYear.
if you would like to serialize only the GraduationYear of the Student, it must make use of a command which the Person will ignore on serialization, a command different from the FULL_INFO, lets call it STUDENT_INFO=2001 - this way when you serialize/deserialize Student with code STUDENT_INFO - it will have only the GraduationYear set, Name and Age will be empty.

Regards,
Atanas

Dechcaudron

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Doubt about derived classes Serialization
« Reply #2 on: June 04, 2014, 10:47:36 AM »
Hey AMarinov,

Great to know how it works, and congratulations on the smart implementation. You guys rock.

wow very thank you. much trick. so reflection. wow
Dechcaudron