R – Using .Net OnSerializingAttribute to alter variable names

.net-2.0netobfuscationserialization

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?

Best Answer

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.