Click here to Skip to main content
13,090,446 members (41,979 online)
Rate this:
Please Sign up or sign in to 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 10-Apr-13 12:28pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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.

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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:
public bool MyIncomprehensibleProperty
{ ... }
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.
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.).

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100