• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

Serialization not working on properties

This one seems so simple, but its not working....

I use the XmlSerializer on the following class, and it only writes out the non-property values.  I have also tried using the serializer on a class with an inherited class, and it only shows the direct members...

      [XmlRoot("Stats")]
      public class PlayerStats
      {
            public PlayerStats()
            {
                  R = H1B = H2B = H3B = HHR = BB = HBP = K = SAC = RBI = SB = PA = 0;
            }

            public int AB
            {
                  get
                  {
                        return PA - BB - HBP - SAC;
                  }
            }
            public int PA; //plate appearances
            public int R,H1B,H2B,H3B,HHR,BB,HBP,K,SAC,RBI,SB;
            public int ReachedBase
            {
                  get
                  {
                        return H1B + H2B + H3B + HHR + BB + HBP;
                  }
            }
            public double OBP
            {
                  get
                  {
                        return ReachedBase / PA;
                  }
            }
            public double AVG
            {
                  get
                  {
                        return (H1B + H2B + H3B + HHR) / AB;
                  }
            }
            public int TOB
            {
                  get
                  {
                        return H1B + 2 * H2B + 3 * H3B + 4* HHR;
                  }
            }
            public double SLG
            {
                  get
                  {
                        return TOB / AB;
                  }
            }

      }
0
bowser17
Asked:
bowser17
2 Solutions
 
AlexFMCommented:
I guess the following values are serialized:

public int PA;
public int R,H1B,H2B,H3B,HHR,BB,HBP,K,SAC,RBI,SB;

Do you expect something more? Only data members (and not properties and functions) can be serialized. Actually, you don't need anything else. Class instance is serialized to some storage. When this instance is loaded, all it's members are restored, and all functions like TOB, SLG are working correctly.
0
 
skpatraCommented:
Serialization only takes care of the instance specific data members. Properties are nothing but special type of funtions. As functions form the blue print of the class and not the instance, these cannot be included in the serialized data.
0
 
AlexFMCommented:
Suppose you have two members: x and y, and Average property. Class instance with x = 2 and y = 4 is serialized. Only x and y are serialized. When class instance is loaded, Average property still returns 3. There is no need to serialize functions and properties, only data members.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
bowser17Author Commented:
Ok, thanks for the feedback.  i could swear that I read on MSDN that fields or properties (read / write) could be serialized... If I added a set function to my properties, would that work?  (just knowing not to use it) (I'll try it later today)...  Also, i saw something about inheriting a serilaiztion class.... I couldn't figure out the examples I saw, but it seemed like you could write your own method to output serialization... any ideas are welcome.

So far between the two, AlexFM looks to have answered the question, but I will split if someone can offer me a solution so I can have the property values output as well.
0
 
isteffenManaging Solution ArchitectCommented:
You need the XmlAttribute attribute from the System.Xml.Serialization namespace. You may also need to make the property hold a variable, but I don't think this is necessary

The following code will definitely work as long as the H1B, H2B, H3B and HHR variables have been included in the serialization.

int m_tob = H1B + 2 * H2B + 3 * H3B + 4* HHR
[XmlAttribute]
public int TOB
{
        get
        {
            return m_tob;
        }
}


0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
Properties will only be added to the output if they have a setter aswell as a getter. If they have one they'll be serialized by default (no need for attributes).

If you don't want a setter for your property that can be used consider this:

public int I{
    get {return m_i;}
    set {throw new InvalidOperationException("Not implemented, needed for serialization"); /* or just plain return will do, but will have people scratching their head why the function isn't working */}
}

I know it looks stupid, but it's the only way.

For those interrested: The properties without a setter aren't transferred because they're supposed to be calculated based upon the internal members. As those will be transferred anyway there should be no need to include the property (which saves processingpower during serialization. Also there is no need to include a variable that cannot be set, as the value cannot be restored to the same type on the other side (as there is no setter that can be called, neither directly, neither through reflection).
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now