Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an internal setter of a property be serialized?

Is there any way to serialize a property with an internal setter in C#?
I understand that this might be problematic - but if there is a way - I would like to know.

Example:

[Serializable] public class Person {     public int ID { get; internal set; }     public string Name { get; set; }     public int Age { get; set; } } 

Code that serializes an instance of the class Person:

Person person = new Person(); person.Age = 27; person.Name = "Patrik"; person.ID = 1;  XmlSerializer serializer = new XmlSerializer(typeof(Person)); TextWriter writer = new StreamWriter(@"c:\test.xml"); serializer.Serialize(writer, person); writer.Close(); 

Result (missing the ID property):

<?xml version="1.0" encoding="utf-8"?> <Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns:xsd="http://www.w3.org/2001/XMLSchema">   <Name>Patrik</Name>   <Age>27</Age> </Person> 
like image 889
Patrik Svensson Avatar asked Jan 07 '09 15:01

Patrik Svensson


People also ask

What can be serialized in C#?

Uses for serializationSending the object to a remote application by using a web service. Passing an object from one domain to another. Passing an object through a firewall as a JSON or XML string. Maintaining security or user-specific information across applications.

Can static class be serialized?

In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI). But, static variables belong to class therefore, you cannot serialize static variables in Java.

Are properties serialized C#?

Only collections are serialized, not public properties.

Can a list be serialized?

List is not but implementation classes like ArrayLists are serializable. You can use them.


2 Answers

If it is an option, DataContractSerializer (.NET 3.0) can serialize non-public properties:

[DataContract] public class Person {     [DataMember]     public int ID { get; internal set; }     [DataMember]     public string Name { get; set; }     [DataMember]     public int Age { get; set; } } ... static void Main() {     Person person = new Person();     person.Age = 27;     person.Name = "Patrik";     person.ID = 1;      DataContractSerializer serializer = new DataContractSerializer(typeof(Person));     XmlWriter writer = XmlWriter.Create(@"c:\test.xml");     serializer.WriteObject(writer, person);     writer.Close(); } 

With the xml (re-formatted):

<?xml version="1.0" encoding="utf-8"?> <Person xmlns:i="http://www.w3.org/2001/XMLSchema-instance"         xmlns="http://schemas.datacontract.org/2004/07/">     <Age>27</Age>     <ID>1</ID>     <Name>Patrik</Name> </Person> 
like image 69
Marc Gravell Avatar answered Sep 24 '22 04:09

Marc Gravell


You can implement IXmlSerializable, unfortunately this negates the most important benefit of XmlSerializer (the ability to declaratively control serialization). DataContractSerializer (xml based) and BinaryFormatter (binary based) could be used as alternatives to XmlSerializer each having its pros and cons.

like image 22
Darin Dimitrov Avatar answered Sep 24 '22 04:09

Darin Dimitrov