views:

148

answers:

2

Hi,

in order to obfuscate application code but still be able to use serialization to save application data, I want to map the names of all serialized members to custom serialization names. I know that I can achieve this by implementing the ISerizableInterface, but in the MSDN they suggest the use of OnDeserializedAttribute, OnSerializingAttribute, OnSerializedAttribute, and OnDeserializingAttribute for serialization. So I would like to use the OnSerializingAttribute to rename my members. The problem is that I can't find a way to explicitly determine the names under which a member is serialized.

Is it possible to explicitly define the names for serialization using the OnSerializingAttribute?

+1  A: 

I think custom serialization and ISerializable interface will be the most efficient way in your case. Check out the sample here. Also here's a good discussion thread towards both ISerializable and serialization attributes you've mentioned.

I would say that if you want to have a full control over the format you should use custom serializer and if you want responding the (de)serialization flow the attributes is the best option.

Denis Vuyka
+1  A: 

(edit: I decided to write this up as a blog entry, as I've seen it be useful a number of times)

I've had users before use protobuf-net for this; this uses Google's dense binary "protocol buffers" wire format, which doesn't include any names on the wire - just numbers. This means that:

  • the names are not exposed on the wire
  • it doesn't matter whether the name in the class at runtime is "DateOfBirth" or "a1v"

This works by assigning an integer tag to members (properties or fields) - for example:

[ProtoMember(4)]
public DateTime DateOfBirth {get;set;}

Only the "4" is used in the serialization, but (importantly) you don't need to maintain lots of serialization code. Can also be hooked into ISerializable very easily if you need to use remoting, and also has a WCF hook if you want.

Disclosure: I'm the author, so I'm biased - but the tool is free, so you don't lose anything by having a look ;-p

Marc Gravell
Sounds that protobuf-net is the right tool for the job, I will give it a try. Although it will obviously break compatibility with existing serialization data files.
phatoni