Click here to Skip to main content
15,888,320 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hey guys,

I've got a question, but I'm pretty sure the answer is no. But is it possible to change the type of a property during serialization.

For example:

If you have a bool property but you wanted to save it as a string in an xml file (for readability purposes). Is this possible?

I've not produced any sample code yet as it is just a theoretical approach at this time. I've done some googling but I've not found any results that say it is/isn't possible so I thought I would ask.
Posted

You are pretty much right about "no", but this is not really related to serialization. You cannot change the declaration of a type during run-time. This fact is more fundamental than serialization. I don't know why would you want it (that's why you are advised to tell us your ultimate goals); I think the whole idea is based on some misconception.

Strictly speaking, you can always change the type of the property in certain sense: if the declared (compile-time) type of the property is some class which has descendant classes, the run-time type of the property could be one or another of those descendant classes. This is a general principle of OOP. However, this aspect is trivial; and I'm sure you understand it. Anyway, you cannot change Boolean to string, or anything like that, anything beyond assignment compatibility.

—SA
 
Share this answer
 
Comments
Pheonyx 11-Apr-13 4:18am    
Hi Sergey,

Thanks for that. I thought it was the case. Another question on the same topic (save making a new thread). Is it possible to have a class with a property that is ONLY used during serialization and cannot be accessed through normal code?

Basically, I am using a "SecureString" to store a password in a settings class. However the password does need to be outputted to a configuration file. I understand this defeats the purpose of the SecureString class, but I want to serialize an encrypted version of the
SecureString. I only want this property to be available for the serialization and not as a normal property (if that makes sense).
Sergey Alexandrovich Kryukov 11-Apr-13 11:45am    
No. There is no such mechanism. Basically, one of the approaches is this: you need to customize serialization itself. Make one property serializable, another non-serializeable. Pre-process the instance of the class before serialization to convert data to serializeable form (encrypt, or whatever), post-process after deserialization.

Are you using Data Contract? If either XML or JSON is fine, drop everything and switch to Data Contract. It has appropriate methods you can implement and include in this mechanism using appropriate attributes.

If you need more help, please remind me by replying to this comment.

—SA
A boolean value is serialised to "false" or "true" - is it this what you want to change? - It does not work.
If you want to change the name of the property, you can use the XmlElementAttribute:
C#
[System.Xml.Serialization.XmlElementAttribute("This_is_the_text_you_want_to_show")]
public bool MyIncomprehensibleProperty
{ ... }
 
Share this answer
 
Comments
Pheonyx 11-Apr-13 4:22am    
Hi Bernhard,

That wasn't quite what I was trying to get at I'm afraid. I used Bool as an example, but in reality I am trying to serialize a "SecureString" which I know is bad practice but is required. I want to secure it as a string that will be encrypted and wondered if you could convert during serialization.
In principle, you may always write you own serialization stuff (please note: writing a reliable serialization facility for all but very simple cases it is a quite difficult task): the most important property of serialization is reversibility: if you serialize an object then you have to be sure you can get it back to life. Provided your serialization code warrants reversibility, it can store object state in the form it likes (you like) more.
 
Share this answer
 
Comments
Pheonyx 11-Apr-13 4:23am    
Hi CP,

Thanks for your response, at this stage looking to write my own serializations stuff is a bit to much for me. I wouldn't know where to start and to do it well I don't really have the time. I will bare this in mind in future though. Thanks.
Sergey Alexandrovich Kryukov 11-Apr-13 11:47am    
No, why? it's really easy to just customize serialization using available mechanism. I do such things with Data Contract. (Please see my comment to my answer.).
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900