?
Solved

Serialize a read-only member

Posted on 2006-05-11
4
Medium Priority
?
254 Views
Last Modified: 2010-04-16
How do I serialize a read-only member?

If I put an empty Set{} block in the class's property, this property is written-out when I serialise it.  But I do not want to have an empty set{} block because then consumers will not see that this property is read-only.


      [System.Xml.Serialization.XmlRoot()]
      public class AddressStruct
      {
            
            private bool _AddressWasRetrievedFromCode = false;      

            [System.Xml.Serialization.XmlElement()]
            public  bool  AddressWasRetrievedFromCode       
            {
                  get {return _AddressWasRetrievedFromCode;}
                  
            }
                  }


Here, element AddressWasRetrievedFromCode is NOT written to the XML.


But withL

      [System.Xml.Serialization.XmlRoot()]
      public class AddressStruct
      {
            
            private bool _AddressWasRetrievedFromCode = false;      

            [System.Xml.Serialization.XmlElement()]
            public  bool  AddressWasRetrievedFromCode       
            {
                  get {return _AddressWasRetrievedFromCode;}
                   set {}
            }
                  }

then the prioperty IS written out.  Howevr I do not want to do it like this because then the consumer thinks that the propery is read-write.


Please advise.

Thanks
DC
0
Comment
Question by:jasww
  • 2
4 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16658705
public  bool  AddressWasRetrievedFromCode      
          {
               get {return _AddressWasRetrievedFromCode;}
               private  set {}
          }
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 16658773
Hi DC,

Why are you putting an empty set block?

Cheers,
Wint.
0
 
LVL 35

Accepted Solution

by:
mrichmon earned 500 total points
ID: 16659699
You cannot do that.  Unfortunately you cannot serialize something that is read only.  That is why they require the psuedo set block.

I found after a lot of research that it was desinged this way because Microsoft feels that anything that can be serialized should be able to be deserialized.  Therefore read only properties are ignored.

So you have 3 choices.

1) You can spoof the compiler to thinking it is read/write by placing an empty set block - but as you have seen then it makes the consumer also see that it is settable, but when they try to use that nothing happens.  Also if they try to deserialize that data is lost.

2) Make a real setter - you lose the fact that this was read-only

3) Do not use auto-serialization, but write a custom function to create your XML stream for you.  We have done this in cases where we KNEW that it needed to be serialized only - never de-serialized - and we wanted the members to remain private.
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 16659937
Ahhh, so that's why you have the empty set block...
I've learned something new :)

Wint
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question